1

I'm trying to write a regular that will check for numbers, spaces, parentheses, + and - this is what I have so far:

/\d|\s|\-|\)|\(|\+/g

but im getting this error: unmatched ) in regular expression any suggestions will help. Thanks

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Jason
  • 49
  • 2
  • @Christian Hayter - You edited out his problem. How are we supposed to answer it now? Please undo. – dlras2 Jul 13 '10 at 18:07
  • @Daniel: actually if you look at first revision, it's like that already. Christian didn't add the \ escapes. I'm also confused now what the question is about. – polygenelubricants Jul 13 '10 at 18:12
  • @polygenelubricants: it looks like there was a SNAFU somewhere; I've rolled back to the original. – Borealid Jul 13 '10 at 18:13
  • @Borealid, @Daniel: so can anyone reproduce OP's problem when the pattern is `/\d|\s|\-|\)|\(|\+/g`? Because that looks fine to me. – polygenelubricants Jul 13 '10 at 18:16
  • I'm also confused... `/\d|\s|\-|\)|\(|\+/g` is in the original source, but Christian's revision shows it being changed *from* `/\d|\s|-|)|(|+/g` *to* `\d|\s|\-|\)|\(|\+/g`, when all he did was reformat the whitespace. Perhaps it's a bug in the SO revision code? – dlras2 Jul 13 '10 at 18:17
  • I think that the original code *was* `\d|\s|-|)|(|+/g`. For some reason, when it's formatted, the escapes appear to be put in? – dlras2 Jul 13 '10 at 18:20
  • @Daniel: let's get OP to confirm what's going on, then if there's a bug I can start a report on meta. I will also delete most of my comments here eventually, since it's just clutter once this matter is resolved. – polygenelubricants Jul 13 '10 at 18:21
  • All I did was add a newline and 3 spaces to change the formatting. Perhaps someone else tried to edit at the same time as me? – Christian Hayter Jul 13 '10 at 19:56

5 Answers5

8

Use a character class:

/[\d\s()+-]/g

This matches a single character if it's a digit \d, whitespace \s, literal (, literal ), literal + or literal -. Putting - last in a character class is an easy way to make it a literal -; otherwise it may become a range definition metacharacter (e.g. [A-Z]).

Generally speaking, instead of matching one character at a time as alternates (e.g. a|e|i|o|u), it's much more readable to use a character class instead (e.g. [aeiou]). It's more concise, more readable, and it naturally groups the characters together, so you can do e.g. [aeiou]+ to match a sequence of vowels.

References


Caveat

Beginners sometimes mistake character class to match [a|e|i|o|u], or worse, [this|that]. This is wrong. A character class by itself matches one and exactly one character from the input.

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
1
/^[\d\s\(\)\-]+$/

This expression matches only digits, parentheses, white spaces, and minus signs. example:

  • 888-111-2222
  • 888 111 2222
  • 8881112222
  • (888)111-2222
  • ...
Laurent Jacquot
  • 591
  • 5
  • 7
1

Here is an awesome Online Regular Expression Editor / Tester! Here is your [\d\s()+-] there.

0

You need to escape your parenthesis, because parenthesis are used as special syntax in regular expressions:

instead of '(': \(

instead of ')': \)

Also, this won't work with '+' for the same reason: \+

Edit: you may want to use a character class instead of the 'or' notation with '|' because it is more readable: [\s\d()+-]

Donald Miner
  • 38,889
  • 8
  • 95
  • 118
0

Try this:

[\d\s-+()]
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
bits
  • 8,110
  • 8
  • 46
  • 55
  • 2
    Although `-` is placed directly after the short hand character class `\s` and therefor needs no escaping, I'd opt to put it (un-escaped) at the start or end of the character class to not cause confusion: `[-\d\s+()]` or `[\d\s+()-]`. – Bart Kiers Jul 13 '10 at 18:08