【发布时间】:2016-11-03 13:13:41
【问题描述】:
我正在浏览一段代码,但遇到了这种语法
str.replace(re,function(raw, p1, p2, p3){
if (!/\/\//.test(p1)) { // <---- this one
//some more code
}
});
我了解测试方法将一个字符串与另一个字符串匹配,并检查它是否存在。但是这个正则表达式/\/\// 匹配字符串是什么?
我检查了正则表达式,然后
\/ matches the character / literally
\/ matches the character / literally
那么if(!//.test(p1)) 在做什么呢?
【问题讨论】:
-
if(!//.test(p1)), 匹配所有不同的东西。 // 包围你的正则表达式,所以 /\/\// 表示 \/\/ 的正则表达式匹配,你需要用 \ 转义 /。所以最后 /\/\// 将与 2/ 连续匹配: //toto
标签: javascript regex string-matching