Javascript匹配“\”字符串中的反斜杠

问题描述:

我使用下面的代码来匹配逗号分隔字符串中的单词

I am using below code to match words in a comma separated string

<script>
var str="testdata, W3\standard,"; 
var patt=/\bW3\\standard/g;
document.write(str.match(patt) );
</script>

但即使我在正则表达式中转义字符串,它也不会给我结果。
任何关于此的帮助

But it does not give me result even though i escape the string in regular expression. Any help on this

该字符串没有 \ 中的字符,因为当你在字符串文字中使用它时会启动转义序列。

The string doesn't have a \ character in it since that starts an escape sequence when you use it in string literals.

你必须转义原文 string literal:

You have to escape the character in your original string literal:

var str="Don't visit the awful W3\\Schools,"; 
var patt1=/\bW3\\Schools/g;
document.write(str.match(patt1));