正则表达式量词练习

来自泡泡学习笔记
跳到导航 跳到搜索

重复模式

// /\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"]