A short question: The following line throws SyntaxError: nothing to repeat
var regexTel = new RegExp('\+[0-9 ]+');
Tools like http://www.regexpal.com/ says that the pattern works fine.
A short question: The following line throws SyntaxError: nothing to repeat
var regexTel = new RegExp('\+[0-9 ]+');
Tools like http://www.regexpal.com/ says that the pattern works fine.
Remember that \
is special in string literals as well as in regular expressions, so a single \
in a string literal escapes what follows it at the string level; so your regex doesn't actually have that \
in it at all. (Since \+
is meaningless in a string literal, it's handled as simpley +
.) You'd need to escape it at the string level with another backslash:
var regexTel = new RegExp('\\+[0-9 ]+');
But, in JavaScript, you don't have to use strings; use a regex literal instead, so you don't have that problem:
var regexTel = /\+[0-9 ]+/;
If what you want is repeat a backslash \, you need to escape it:
var regexTel = new RegExp('\\+[0-9 ]+');
As others point out, it's either to add the slashes in the string you pass to the regex:
var regexTel = new RegExp('/\+[0-9 ]+/');
Or either use the more compact version that removes the use of the RegExp
object:
var regexTel = /\+[0-9 ]+/;
You have to escape "+" sign. In your regex "\" sign is recognized as a delimiter. Go with:
var regexTel = new RegExp(/\+[0-9 ]+/);
try this
var regexTel = new RegExp(/\+[0-9 ]+/);