比如在php里面
<code class="lang-php">preg_prelace("/(.+?)<\/\\1>/is", $text);</code>
这个在我可以用\\1
来引用第一个参数,在js的正则表达式里可以这么用吗?
回复内容:
比如在php里面
<code class="lang-php">preg_prelace("/(.+?)<\/\\1>/is", $text);</code>
这个在我可以用\\1
来引用第一个参数,在js的正则表达式里可以这么用吗?
用\n
就可以了,比如\1
,\2
,例子:
<code>var regexp = /(['"])[^'"]*\1/;c<mark>6来源gaodaimacom搞#^代%!码网</mark><strong>搞gaodaima代码</strong>onsole.log( regexp.test("'1'"), regexp.test('"2"'), regexp.test('"3\'')); /* true true false */</code>
当然可以啊
<code>/(.+?)<\/\1>/.exec("xxx")</code>
PS. 因为不是在字符串里,所以你不需要两个反斜线
可以的,不过JS里面String.replace(reg,newStr)
可以直接使用正则字面量的,所以不用包在字符串里面。
1 2 n 在正则里面第几个子串;可以用 $1 $2 $n 指匹配到的字符串内容
比如,把匹配到的标签替换成空字符串:
<code>var str = '<p>hello world</p>';str.replace(/(.+)<\/\1>/i,'');//</code>