如何在regexp中避开星号? [英] How to escape asterisk in regexp?

查看:155
本文介绍了如何在regexp中避开星号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用模式 * 1 * 。我试过 \ * 1\ * ,但它不工作。问题在哪里?

I want to use the pattern *1*. I have tried \*1\*, but it doesn't work. Where is the problem?

推荐答案

您必须使用反斜杠将其转义:

You have to escape it with a backslash:

/\*1\*/

否则,RegExp中的未转义的 * 将意味着:匹配0或更多的前一个字符组。

Otherwise, an unescaped * in a RegExp will mean: Match 0 or more of the Preceding Character Group.

更新:

如果您使用 RegExp 构造函数,请执行以下操作:

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.

这篇关于如何在regexp中避开星号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆