3

This is what I do:

var input_string = "(1) (1) test.txt";
var reg = new RegExp('^\\(\\d+\\)\\s+' + input_string,'i');
var tested_string = "(1) (1) (1) test.txt";
alert(reg.test(tested_string)); // returns false

I expect it to return true, but it does not. I wonder, what I'm doing wrong?

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
Jacobian
  • 10,122
  • 29
  • 128
  • 221

2 Answers2

6

You have to escape all parenthesis, and you are not. Adding the \\ to the ( on the input string would make it work:

var input_string = "\\(1\\) \\(1\\) test.txt"; // fixed
var reg = new RegExp('^\\(\\d+\\)\\s+' + input_string,'i');
var tested_string = "(1) (1) (1) test.txt";
alert(reg.test(tested_string)); // returns true now

would work.

If you need to use arbitrary strings in the regex, you need to escape them. I suggest you use a function like the one below before adding it to the RegExp object:

function escapeRegExp(stringToGoIntoTheRegex) {
    return stringToGoIntoTheRegex.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}

In your case it could be like:

var input_string = "(1) (1) test.txt";
var reg = new RegExp('^\\(\\d+\\)\\s+' + escapeRegExp(input_string),'i'); // using it here
var tested_string = "(1) (1) (1) test.txt";
alert(reg.test(tested_string)); // returns true as expected

See demo fiddle.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • Thanks! Your answer was first, and I will accept it, as soon as stackoverflow allows me to do it -) – Jacobian Jun 18 '15 at 20:50
  • 1
    Np. Make sure you check the `escapeRegExp()` function. It could prove itself useful in the future! – acdcjunior Jun 18 '15 at 20:52
  • There is no need to escape `/` at all. It has no special meaning in regex if you construct it via RegExp constructor. – nhahtdh Jun 19 '15 at 03:24
  • @nhahtdh There are several implementations of that function around the internet, some include `/`, some don't. I do agree escaping it seems to be unecessary, but some [do report](http://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript#comment47308991_9310752) having problems when not escaping. So, yeah, I guess depending on the concrete situation we can make it "optional". – acdcjunior Jun 19 '15 at 14:20
  • @acdcjunior Do note that the author of the answer doesn't update the post after comment, and if you read his blog, he write stuffs about ES6 regex. – nhahtdh Jun 19 '15 at 15:38
  • @nhahtdh Yes, we have no evidence that the `/` requires escaping other than complaints about tools and whatnot. So, ultimately, if one doesn't want to escape the `/`, one doesn't have to, though escaping it doesn't hurt either - as far as evidences go as well. – acdcjunior Jun 19 '15 at 17:14
5

Because you do not escape the ( and ) which makes them capture groups. Also the period also needs to be escaped so it is not a match anything character.

    var input_string = "\\(1\\) \\(1\\) test\\.txt";
    var reg = new RegExp('^\\(\\d+\\)\\s+' + input_string,'i');
    var tested_string = "(1) (1) (1) test.txt";
    alert(reg.test(tested_string)); // returns false
epascarello
  • 204,599
  • 20
  • 195
  • 236