正则表达式量词练习

来自泡泡学习笔记
BrainBs讨论 | 贡献2023年8月14日 (一) 15:17的版本 (创建页面,内容为“ == 重复模式 == <pre>// /\w+a+/; "This is Spartaaaaaaa" ==> "Spartaaaaaaa"</pre> == 计算字符集 == <pre>"Why do I have to learn multiplication table?" // /\b\w\b/ "I" // /\b\w{1,6}\b/ [ "Why", "do", "I", "have", "to", "learn", "table" ] // /\b\w{13,}\b/ ["multiplication"]</pre> == 可选字符 == <pre>// /\w+ou?r/ // \w+ 至…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

重复模式

// /\w+a+/;
"This is Spartaaaaaaa"
==>
"Spartaaaaaaa"

计算字符集

"Why do I have to learn multiplication table?"

// /\b\w\b/
"I"

// /\b\w{1,6}\b/
[ "Why", "do", "I", "have", "to", "learn", "table" ]

// /\b\w{13,}\b/
["multiplication"]

可选字符

// /\w+ou?r/
// \w+ 至少一个字母
// o   后跟o
// u?  后跟1个或0个u
// r   后跟r

"He asked his neighbour a favour."
==>
["neighbour", "favour"]

"He asked his neighbor a favor."
==>
["neighbor", "favor"]

贪婪与非贪婪的

"I must be getting somewhere near the centre of the earth."

// /[\w ]+/
// [\w ]    一个字母
// +        至少1次
["I must be getting somewhere near the centre of the earth"]

// /[\w ]+?/
["I", " ", "m", "u", "s", "t", " ", "b", "e", " ", "g", "e", "t", "t", "i", "n", "g", " ", "s", "o", "m", "e", "w", "h", "e", "r", "e", " ", "n", "e", "a", "r", " ", "t", "h", "e", " ", "c", "e", "n", "t", "r", "e", " ", "o", "f", " ", "t", "h", "e", " ", "e", "a", "r", "t", "h"]