0

I have this regex expression var re = /(?:\d{3}|\(\d{3}\))([\w-\/\.]?)\d{3}\1\d{4}/;, however, the \w whitespace doesn't work on this test console.log(re.test('123 456 7890'));

Here is my jsfiddle: http://jsfiddle.net/Bqb22/

Raptrex
  • 3,915
  • 10
  • 49
  • 61
  • @M42 Does this regex look comprehensive? I wouldn't call this a duplicate. It's true that the OP might benefit from looking at a more comprehensive solution, but that doesn't make this a duplicate. – Platinum Azure Feb 18 '14 at 19:17
  • 1
    Psst, "American" phone numbers are continent-wide, including most of the Caribbean. – Diodeus - James MacFarlane Feb 18 '14 at 19:20
  • Not exactly connected with your question, but I recommend http://refiddle.com/ for testing regex. For me testing regex there is easier and faster than in the code. – Konrad Gadzina Feb 18 '14 at 19:34

1 Answers1

8

You shouldn't use \w for whitespace. Use \s instead. (\w is word character, the same as [0-9A-Za-z_], and should not be used to indicate whitespace).

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • Haha. Nice quick edit. I was testing your previous answer and it was not working. Now it does. Proof is in the pudding: `var a = "123 456 7890"; a.match(/(?:\d{3}|\(\d{3}\))([\s-\/\.]?)\d{3}\1\d{4}/); // returns ["123 456 7890", " "]`. +1 BTW – Jess Feb 18 '14 at 19:21
  • 2
    **\s = space** and **\w = word**. But sometimes you think \w is whitespace! ;) – Jess Feb 18 '14 at 19:23
  • 1
    So I was looking at a table and reading the description for \s but kept typing \w for some reason... – Raptrex Feb 18 '14 at 19:27