Strange behavior: Dynamic expressions with RegExp Object in RegExp.match (Javascript)

Posted by NeDark on Stack Overflow See other posts from Stack Overflow or by NeDark
Published on 2010-05-08T19:13:19Z Indexed on 2010/05/08 19:18 UTC
Read the original article Hit count: 295

Filed under:
|

I have detect a strange behavior in regexps created with the RegExp object:

With this code:

var exp1 = /./;
var exp2 = new RegExp('.');
?
var test1 = exp1.test('large\n\ntext..etc.');
var test2 = exp2.test('large\n\ntext..etc.');
?
var match1 = 'large\n\ntext..etc.'.match(exp1);
var match2 = 'large\n\ntext..etc.'.match(exp2);

...the result is:

test1 = true
test2 = true
?
match1 = 'l' (first match)
match2 = null

With the regexp maked with the regexp object from a string it finds nothing...

Why does this happends?

Thanks!!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex