I'm trying to use the following pattern with RegExp, but it doesn't seem to work right. When I take the same expression and use it directly, the expression works correctly.
The expression is designed to find numbers or numbers with letter at the end.
The example below should not find any match in the string.
The RegExp example finds "ld". Why? What am I doing wrong?
HTML Code:
var str = "printf(\"Hello World\");";
var myPattern = new RegExp("([^\w][\d]+[\w]*)|(^[\d]+[\w]*)");
alert(str.search(myPattern));
alert(str.search(/([^\w][\d]+[\w]*)|(^[\d]+[\w]*)/));
alert(str.search(/^\d+\w$/));
^ begining of string
\d+ one or more numbers
\w one number or letter only
$ END OF STRING
I'm sorry that's not what I asked.
My expression, when used through RegExp doesn't work like it should, and when used directly with / / it works great, why???
Hmm - I guess 'myPattern' does not interpolate within an alert
Code:
var str = "printf(\"Hello World\");";
var myPattern = new RegExp("([^\w][\d]+[\w]*)|(^[\d]+[\w]*)");
var result = str.search(myPattern);
alert(str.search(result));
Just for reference,
Your regex will match a lot more than ONLY numbers follows by one letter or number.
Bookmarks