Javascript匹配“\”字符串中的反斜杠 [英] Javascript matching "\" backslash in a string
本文介绍了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.
你必须转义原文<中的字符/ strong> 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));
这篇关于Javascript匹配“\”字符串中的反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文