I have this piece of VBNet code that i would like to translate into javascript:
Dim phone_check_pattern = "^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.//-]|\([ 0-9.//-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.//-]|\([ 0-9.//-]+\)))?$"
System.Diagnostics.Debug.WriteLine(System.Text.RegularExpressions.Regex.IsMatch("test input", phone_check_pattern))
my translated result:
var phone_check_pattern = "^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.//-]|\([ 0-9.//-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.//-]|\([ 0-9.//-]+\)))?$";
alert(new RegExp(phone_check_pattern).test("test input"))
However when i run it it has error Uncaught SyntaxError: Invalid regular expression:: Nothing to repeat
(my VbNet code doesn't have any error though)
Does anyone know what's causing the problem?
Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.
In JavaScript, a Regular Expression (RegEx) is an object that describes a sequence of characters used for defining a search pattern. For example, /^a...s$/ The above code defines a RegEx pattern. The pattern is: any five letter string starting with a and ending with s .
For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter. In a character set a ^ character negates the following characters.
By default, regular expressions will match any part of a string. It's often useful to anchor the regular expression so that it matches from the start or end of the string: ^ matches the start of string. $ matches the end of the string.
The backslash character in JavaScript strings is an escape character, so the backslashes you have in your string are escaping the next character for the string, not for the regular expression. So right near the beginning, in your "^(\+?
, the backslash there just escapes the +
for the string (which it doesn't need), and what the regexp sees is just a raw +
with nothing to repeat. Hence the error.
Fortunately, JavaScript has a literal syntax for regular expressions (delimited with /
characters), which would probably be a better starting point for you:
var re = /^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.\/-]|\([ 0-9.\/-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.\/-]|\([ 0-9.\/-]+\)))?$/;
alert(re.test("test input"));
Then at least the backslashes are escaping in the regex, not the string. (Note that since /
is the delimiter for the regular expression literal, we have to escape it (with a backslash).)
I haven't exhaustively reviewed the actual regexp, but that should get you started.
More about regular expression literals in the spec, of course, and here on MDC.
I'm not sure but try to use \\
instead of \
in your javascript code. Seen some samples that did this.
The double-slash, as everyone said, is important. This works:
var phone_check_pattern = "^(\\+?|(\\(\\+?[0-9]{1,3}\\))|)"+
"([ 0-9.//-]|\\([ 0-9.//-]+\\))+"+
"((x|X|((e|E)(x|X)(t|T)))([ 0-9.//-]|\\([ 0-9.//-]+\\)))?$";
var re = new RegExp(phone_check_pattern);
say(re.test("test input"));
say(re.test("(415) 828-3321"));
say(re.test("+1 (212) 828-3321"));
say(re.test("da828-3321"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With