如何正确转义正则表达式中的字符
我想在字符串内进行字符串搜索。简单地说MySTR.search(Needle)
。
当此 needle
字符串包含特殊的正则表达式字符(如 *、+ 等)时,就会出现问题。它失败并出现错误无效量词
。
我浏览了网页,发现可以使用 \Q some string \E
转义字符串。
然而,这并不总是产生期望的行为。例如:
var sNeedle = '*Stars!*';
var sMySTR = 'The contents of this string have no importance';
sMySTR.search('\Q' + sNeedle + '\E');
结果为-1。好的。
var sNeedle = '**Stars!**';
var sMySTR = 'The contents of this string have no importance';
sMySTR.search('\Q' + sNeedle + '\E');
结果是“量词无效”。发生这种情况是因为 2 个或更多特殊字符相互“接触”,因为:
var sNeedle = '*Dont touch me*Stars!*Dont touch me*';
var sMySTR = 'The contents of this string have no importance';
sMySTR.search('\Q' + sNeedle + '\E');
可以正常工作。
我知道我可以创建一个函数 escapeAllBadChars(sInStr)
并在每个可能的特殊正则表达式字符之前添加双斜杠,但我想知道是否有更简单的方法来做到这一点?
I want to do a string search inside a string. Simply saying MySTR.search(Needle)
.
The problem occurs when this needle
string contains special regex characters like *,+ and so on. It fails with error invalid quantifier
.
I have browsed the web and found out that string can be escaped with \Q some string \E
.
However, this does not always produce the desired behavior. For example:
var sNeedle = '*Stars!*';
var sMySTR = 'The contents of this string have no importance';
sMySTR.search('\Q' + sNeedle + '\E');
Result is -1. OK.
var sNeedle = '**Stars!**';
var sMySTR = 'The contents of this string have no importance';
sMySTR.search('\Q' + sNeedle + '\E');
Result is "invalid quantifier". This happens because 2 or more special characters are 'touching' each other, because:
var sNeedle = '*Dont touch me*Stars!*Dont touch me*';
var sMySTR = 'The contents of this string have no importance';
sMySTR.search('\Q' + sNeedle + '\E');
Will work OK.
I know I could make a function escapeAllBadChars(sInStr)
and just add double slashes before every possible special regex character, but I'm wondering if there is a simpler way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
\Q...\E
在 JavaScript 中不起作用(至少,它们不会转义任何内容...),如您所见:生成:
正如您在 Ideone。
以下字符需要转义:
(
)
[
{
*
+
.
$
^
\
|
?
所以,类似这样的事情就可以了:
不,
]
并且}
不需要转义:它们没有特殊含义,只有它们的开头对应部分。请注意,当使用文字正则表达式
/.../
时,您还需要转义/
字符。但是,/
不是正则表达式元字符:在RegExp
对象中使用它时,它不需要转义。\Q...\E
doesn't work in JavaScript (at least, they don't escape anything...) as you can see:produces:
as you can see on Ideone.
The following chars need to be escaped:
(
)
[
{
*
+
.
$
^
\
|
?
So, something like this would do:
No,
]
and}
don't need to be escaped: they have no special meaning, only their opening counter parts.Note that when using a literal regex,
/.../
, you also need to escape the/
char. However,/
is not a regex meta character: when using it in aRegExp
object, it doesn't need an escape.我只是尝试一下 Javascript,但是您是否有理由需要使用正则表达式引擎呢?怎么样
I'm just dipping my feet in Javascript, but is there a reason you need to use the regex engine at all? How about
我进行了一次快速的 Google 搜索,看看那里有什么,看来你有一些转义正则表达式字符的选项。根据一页,可以定义&运行如下所示的函数来转义有问题的字符:
或者,您可以尝试使用单独的库,例如 XRegExp,它已经处理您试图重新解决的细微差别。
I performed a quick Google search to see what's out there and it appears that you've got a few options for escaping regular expression characters. According to one page, you can define & run a function like below to escape problematic characters:
Alternatively, you can try and use a separate library such as XRegExp, which already handles nuances you're trying to re-solve.
https://stackoverflow.com/a/6969486/151312 的重复项
根据 MDN 是正确的(请参阅帖子中的解释)多于):
Duplicate of https://stackoverflow.com/a/6969486/151312
This is proper as per MDN (see explanation in post above):