I want to use the pattern *1*
. I have tried \*1\*
, but it doesn't work. Where is the problem?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You have to escape it with a backslash:
/\*1\*/
Otherwise, an unescaped *
in a RegExp will mean: Match 0 or more of the Preceding Character Group.
Update:
If you use the RegExp
constructor, do it this way:
new RegExp("\\*1\\*")
You have to double-escape the backslashes because they need to be escaped in the string itself.
回答2:
need to use a backslash \
as the escape character in regexes.