Javascript regular expressions as functions?

Obviously, in Firefox 3.6 it was legal:

/[0-9]{3}/('23 2 34 678 9 09') 

and the result was "678".

FF8 does not have. What is the correct syntax now?

+7
source share
3 answers

Do you want to

 /[0-9]{3}/.test('23 2 34 678 9 09'); 

or

 /[0-9]{3}/.exec('23 2 34 678 9 09'); 
+6
source

I don't know why you need this syntax, but here is something for lulz:

 RegExp = (function(){ var old = RegExp; return function(){ return old.prototype.exec.bind( old.apply( this, arguments ) ); }; })() 

Then:

 new RegExp( "[0-9]{3}" )('23 2 34 678 9 09') //["678"] 

Please note that when using literals, the hack constructor will not be called, so it only works when using new RegExp ; P

+3
source

All Articles