Reputation: 23
I wrote a regular expression
^([+/-]?([0-9]+(.)?)|([0-9]*.[0-9]+))$
I create it by two ways
var _regex = "^([+/-]?([0-9]+(\.)?)|([0-9]*\.[0-9]+))$";
var _regexFloat = new RegExp(_regex);
and
var _regexFloat = /^([+/-]?([0-9]+(\.)?)|([0-9]*\.[0-9]+))$/ ;
the testing data is "1a" and "a1".
at the second way, it work fine.
but in the first way, it returns true.
Can anyone suggest me if I have something wrong.
Thanks very much.
Environment:
Windows Server 2003
IE 6
Upvotes: 2
Views: 101
Reputation: 39057
I believe you'll need to escape those backslashes in the string (in the first version).
Try this:
var _regex = "^([+/-]?([0-9]+(\\.)?)|([0-9]*\\.[0-9]+))$";
var _regexFloat = new RegExp(_regex);
Upvotes: 5