6

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?

4
  • @Pacerier Can you tell us what the regEx is supposed to do. Commented May 19, 2011 at 8:11
  • 1
    @sra: He's taking on the formidable (some would even say Quixotic) task of validating a phone number entered in a free-form field. Not for the faint of heart, and prone to all sorts of problems. Commented May 19, 2011 at 8:13
  • 1
    @sra its stated in the question, within the code Commented May 19, 2011 at 8:55
  • @Pacerier Oh, yes. I should read more closely next :) Commented May 19, 2011 at 8:58

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure but try to use \\ instead of \ in your javascript code. Seen some samples that did this.

Comments

1

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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.