1

I have the following bit of code :

var stringToMatch = "De$mond. \(blah)";
var pattern = "^" + stringToMatch;
var regex = new RegExp(pattern, "i");

return regex.test("testing De$mond.");

Now I need to escape stringToMatch before using it in pattern

A solution I found here suggest this method if I understand correctly :

var stringToMatch = "De$mond. \(blah)";
stringToMatch = stringToMatch.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

var pattern = "^" + stringToMatch;
var regex = new RegExp(pattern, "i");

return regex.test("testing De$mond.");

Why can't I simply escape all of the characters in stringToMatch instead?

e.g.

  var stringToMatch = "De$mond. \(blah)";
  var stringToMatchAsArrayOfChars = [];
  for (var i = 0; i < stringToMatch.length; i++)
  {
    stringToMatchAsArrayOfChars.push(stringToMatch.substr(i, 1));
  }
  var stringToMatchEscaped = "";
  for (var i = 0; i < stringToMatchAsArrayOfChars.length; i++)
  {
    if (stringToMatchAsArrayOfChars[i] !== " ")
    {
      stringToMatchEscaped = stringToMatchEscaped + "\\" + stringToMatchAsArrayOfChars[i];
    }
    else
    {
      stringToMatchEscaped = stringToMatchEscaped + " ";
    }
  }

  var pattern = "^" + stringToMatch;
  var regex = new RegExp(pattern, "i");

  return regex.test("testing De$mond.");

I understand that the above method is much more verbose but what it basically does is :

  var stringToMatchEscaped = "\D\e\$\m\o\n\d\. \\\(\b\l\a\h\)";

But it's not working. Why is that?

And, also, is there some other way of escaping stringToMatch other than the one suggested in the link I provided? i.e. without specifying which characters to escape like it's being dones with /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g ?

Community
  • 1
  • 1
  • 2
    Because if you escaped `dog`, you'd get `\d\o\g`. `\d` is shorthand for `[0-9]`. `\D` matches `[^0-9]`. – Blender Jun 11 '13 at 20:09
  • hmm.. didn't realise that :-) you're right. So I absolutely need to escape a group of specific characters, there is no other way around that without referring to specific characters? – The Norton Commander Jun 11 '13 at 20:11
  • Why do you need to do this in the first place? What are you actually trying to accomplish with this code? – Blender Jun 11 '13 at 20:12
  • it's just a simple search engine that I'm building. I have an array of strings and the user can type in a word. I then look for a match inside that array, but given that I used regex to look for a match I obviously need to escape the characters in the user's input – The Norton Commander Jun 11 '13 at 20:14
  • @TheNortonCommander if you are matching the input literally, why don't you just use [`indexOf`](http://www.w3schools.com/jsref/jsref_indexof.asp) instead of chasing this through the regex engine? – Martin Ender Jun 11 '13 at 21:17

1 Answers1

0

here is a simple regexp to make safe regexp patterns from string input.

  var pattern= "De$mond.";
  var regex = new RegExp(pattern.replace(   /([.*+?^${}()|[\]\/\\])/g   , '\\$1'), "i");

  regex.test("testing De$mond. string here."); // === true

note that this means you cannot use the "wildcards" or any RegExp syntax, but you'll end up with a real regexp that will perform a literal match from the source text to the pattern.

dandavis
  • 16,370
  • 5
  • 40
  • 36