正则表达式用边界限定符来表明匹配操作在什么位置(或边界)发生。
\b 指定单词的边界。即一个单词的开始或结尾。 同样,\B指定非单词边界。
// ["cat"]
"The cat scattered his food all over the roome.".match(/\bcat\b/g);
// ["cat", "cat"]
"The cat scattered his food all over the roome.".match(/cat/g);
^匹配字符串的开头。 $匹配字符串的结尾。 注意:^出现在一个字符串集合中时(左方括号[后面),表示求非。
"<?xml version=\"1.0\"></xml>".match(/^<\?\w*/g); // ["<?xml"]
"<?xml version=\"1.0\"></xml>".match(/<\/xml>$/g); // ["</xml>"]
注意:有正则表达式不支持(?m)。