How to escape asterisk in regexp?

强颜欢笑 提交于 2019-11-29 11:05:06

问题


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.



来源:https://stackoverflow.com/questions/15205536/how-to-escape-asterisk-in-regexp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!