C.Fasolin
C.Fasolin

Reputation: 319

JavaScript Regex mismatch

I have this code:

var regExp=new RegExp("(\/|-|.)+","g");

var t1=regExp.test(new Date());
alert(t1);//result true

var t2=regExp.test("19/03/1986");
alert(t1);//result true

why, first alert, return true value? There is not any charatter defined into regex pattern...

Thanks

Upvotes: 0

Views: 505

Answers (2)

C.Fasolin
C.Fasolin

Reputation: 319

I found a solution:

var regExp= /(\/|-|\.)/g;
var t1=regExp.test(new Date());
alert(t1);// return false
var t2=regExp.test("19/03/1986");
alert(t2);// return true

Thanks

Upvotes: 0

Byron
Byron

Reputation: 56

. means any char in regexp, so any char can match your exp

you can change it to \d{4}(\/|-|\.)\d{2}(\/|-|\.)\d{2}

Maybe this can help you understand what you write

Regexper

enter image description here

and remove g

Upvotes: 1

Related Questions