1

I've got this regular expression for validating phone numbers

^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.//-]|\([ 0-9.//-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.//-]|\([ 0-9.//-]+\)))?$

I dugged it out from my C#/vb library and now i want to translate it into javascript. But it has syntax error (i suspect it is something due to the // characters). my attempt:

$IsPhone = function (input) {
  var regex = new window.RegExp("^$|^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.//-]|\([ 0-9.//-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.//-]|\([ 0-9.//-]+\)))?$", "");
  return regex.test(input.trim());

};
alert($IsPhone("asd"));
11
  • Can you please give some context about what your expression is attempting to match? It will make it easier to debug. Commented May 9, 2011 at 11:29
  • Have you tried to escape the slashes? Commented May 9, 2011 at 11:29
  • 1
    what is this? a game of "spot the difference"? Anyway, if it is a syntax error I would try using JSLint. Commented May 9, 2011 at 11:29
  • 1
    Why would you need to double the slashes anyway? AFAICS, they're in a character class, so a single one should do. Commented May 9, 2011 at 11:34
  • 1
    My guess is you wanted to write [ 0-9./-] instead of [ 0-9.//-] (putting it twice doesn't make any sense, and might cause the regex to be flawed) Commented May 9, 2011 at 11:34

2 Answers 2

4

Your problem has nothing to do with comments. You're just mixing up the two different ways of creating RegExp objects.

When you create a RegExp object in JavaScript code, you either write it as a string literal which you pass to a RegExp constructor, or as a regex literal. Because string literals support backslash-escape sequences like \n and \", any actual backslash in the string has to be escaped, too. So, whenever you need to escape a regex metacharacter like ( or +, you have to use two backslashes, like so:

var r0 = "^$|^(\\+?|(\\(\\+?[0-9]{1,3}\\))|)([ 0-9./-]|\\([ 0-9./-]+\\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9./-]|\\([ 0-9./-]+\\)))?$";
var regex0 = new RegExp(r0, "");

The forward-slash has no special meaning, either to regexes or strings. The only reason you ever have to escape forward-slashes is because they're used as the delimiter for regex literals. You use backslashes to escape the forward-slashes just like you do with regex metacharacters like \( or \+, or the backslash itself: \\. Here's the regex-literal version of your regex:

var regex1 = /^$|^(\+?|(\(\+?[0-9]{1,3}\))|)([ 0-9.\/-]|\([ 0-9.\/-]+\))+((x|X|((e|E)(x|X)(t|T)))([ 0-9.\/-]|\([ 0-9.\/-]+\)))?$/;
Sign up to request clarification or add additional context in comments.

Comments

1

from Errors translating regex from .NET to javascript

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.

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.