Javascript Regular Expressions
Posted In:
Javascript
,
RegularExpressions
.
By Sid
Last night I spent an anguishing two hours on this small problem, before getting badly irritated and watching half of a really horrid movie.
The Regexp.test(String) function in Javascript is called as:
/^[A-Za-z]/.test("abcd");
The above expression is to test weather a string starts with a letter or not.
This is NOT the same as:
var x = "/^[A-Za-z]/";
var y = "radio-gaga";
Regexp(x).test(y);
When you pass anything to the Regexp() constructor make sure it's not delimited by the '/'s. Ergo, what works is:
var x = "^[A-Za-z]";
var y = "radio-gaga";
Regexp(x).test(y);
The Regexp.test(String) function in Javascript is called as:
/^[A-Za-z]/.test("abcd");
The above expression is to test weather a string starts with a letter or not.
This is NOT the same as:
var x = "/^[A-Za-z]/";
var y = "radio-gaga";
Regexp(x).test(y);
When you pass anything to the Regexp() constructor make sure it's not delimited by the '/'s. Ergo, what works is:
var x = "^[A-Za-z]";
var y = "radio-gaga";
Regexp(x).test(y);
0 Responses to Javascript Regular Expressions
Something to say?