62

I've got a situation where I want to get a regexp from the user and run it against a few thousand input strings. In the manual I found that the RegExp object has a .compile() method which is used to speed things up ins such cases. But why do I have to pass the regexp string to it again if I already passed them in the constructor? Perhaps constructor does the compile() itself?

Vilx-
  • 104,512
  • 87
  • 279
  • 422

4 Answers4

100

The RegExp().compile() method is deprecated. It's basically the same as the constructor, which I assume is why it was deprecated. You should only have to use the constructor nowadays.

In other words, you used to be able to do this:

var regexp = new RegExp("pattern");
regexp.compile("new pattern");

But nowadays it is not any different from simply calling:

var regexp = new RegExp("pattern");
regexp = new RegExp("new pattern");
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Dan Lew
  • 85,990
  • 32
  • 182
  • 176
7

And with Opera 11, running RegExp.compile() will actually cause errors.

Evidently, when Opera "compiles" a regex, it wraps the re.source string in forward slashes (e.g. re.source == "^(.)" becomes "/^(.)/"). If you manually compile the regex, Opera doesn't recognize this fact and goes ahead and compiles it again (re.source becomes "//^(.)//"). Each compile results in an extra set of forward slashes, which changes the meaning of the regular expression and results in errors.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
ridgerunner
  • 33,777
  • 5
  • 57
  • 69
6

You have to compile your regex first to use it if you are using /, try this out:

var regex=new RegExp('/[a-zA-Z]/')

console.log("not compiled with escape /", regex.test("ciao") )

regex.compile()

console.log("compiled", regex.test("ciao") )

var regex=new RegExp('[a-zA-Z]')

console.log("not compiled, but no escape /", regex.test("ciao") )
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
  • 3
    Wow, that's interesting. I just checked it, and you're right! It's not mentioned in MDN though. – Vilx- Nov 03 '16 at 16:23
  • 2
    You shouldn't use forward slashes when using the `RegExp` constructor. Forward slashes are used to start and end `RegExp` *literal*, they are not part of the pattern (unless you actually mean to match literal forward slashes). IMO, this is actually buggy behaviour in `compile` and shouldn't be relied on. – Rob Hogan Sep 17 '20 at 13:48
  • 3
    To make the point - `new RegExp('/[a-zA-Z]/').test('/a/')` *is* `true`. The forward slashes are properly interpreted as part of the pattern. `compile()` actually changes this (Chrome 85), which I don't think it should. However, `compile()` itself is now marked deprecated in MDN. – Rob Hogan Sep 17 '20 at 13:57
4

As far as i can tell all RegExp.compile does is replace the underlying regular expression of a RegExp object. I think compile may have had value in the past, but all modern JS engines "compile" the regex on first call and cache that "compiled" version.

olliej
  • 35,755
  • 9
  • 58
  • 55
  • 5
    Which engines exactly do cache regular expressions? And do they always do it? How long is an expression kept in cache? I'm particularly interested in V8. – CodeManX Aug 31 '15 at 14:24