Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

javascript - How to properly escape characters in regexp

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Q...E doesn't work in JavaScript (at least, they don't escape anything...) as you can see:

var s = "*";
print(s.search(/Q*E/));
print(s.search(/*/));

produces:

-1
0

as you can see on Ideone.

The following chars need to be escaped:

  • (
  • )
  • [
  • {
  • *
  • +
  • .
  • $
  • ^
  • |
  • ?

So, something like this would do:

function quote(regex) {
  return regex.replace(/([()[{*+.$^\|?])/g, '\$1');
}

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 a RegExp object, it doesn't need an escape.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...