0

I'm currently studying regular expression. I didn't really know / understand how to write an expression with 2 condition. Or do I have to write 2 regex for each case?

What I am on is,

I have a txtMobile field in my html field

<label for="txtMobile">Mobile:*</label>
<input id="txtMobile" type="text" name="txtMobile" size="22" maxlength="22">

In my javascript file I'm trying to create a expressions with the following rule

Format: (+##)###-###-####/#### extension is optional and it may start without country code.

Is there any way to do in a single expression or do I have to type if conditions for each case in my javascript file?

Here is what I did /^([0-9]{3})\-([0-9]{3})\-([0-9]{4})\/([0-9]{4})$/

Vivin Paliath
  • 87,975
  • 37
  • 202
  • 284
A. Mesut Konuklar
  • 575
  • 3
  • 10
  • 25
  • checkout this question: http://stackoverflow.com/questions/3378773/can-i-use-an-or-in-regex-without-capturing-whats-enclosed – orustammanapov Nov 06 '13 at 20:43

3 Answers3

0

Make the groups optional by using ? which means "zero or one of the preceding":

/^([0-9]{3}-)?([0-9]{3})\-([0-9]{4})(\/[0-9]{4})?$/

? is a modifier like * (zero or more of the preceding) and + (one or more of the preceding).

This makes both groups optional which means that numbers with all three components (country code, number, extension), numbers with either of the optional components (number + extension, country code + number), and numbers with neither of the optional components, will be accepted by the regex.

EDIT

Your mistake in the regex in your comment is that the ? is after the escaped ), which means zero or one of an actual ) and not of a group. What you need is:

^(\(\+[0-9]{2}\))?([0-9]{3}-)?([0-9]{3})\-([0-9]{4})(\/[0-9]{4})?$
Vivin Paliath
  • 87,975
  • 37
  • 202
  • 284
  • Hello, thanks for the comment @vivin-paliath ! I changed my regex but it still doesnt work. I wanted both (+90) part and /#### part to be optional in this one `^\(\+([0-9]{2})\)?([0-9]{3}-)?([0-9]{3})\-([0-9]{4})(\/[0-9]{4})?$`. But I think I made a mistake, could you tell me what part I failed ? – A. Mesut Konuklar Nov 06 '13 at 20:50
0

Use ? to indicate an optional atom.

/^(\(+[0-9]{2}\))?...

Here I have made the preceding group optional, and notice how the parentheses group several atoms into a single atom. The literal parentheses are escaped. The is the start of a regex that allows an optional country code following your format. Allowing for an optional extension is left as an exercise to the reader. :)

Roger Pate
  • 2,247
  • 13
  • 15
0

Like the other guys said ? matches 0 or 1 of the proceeding token.

/^(\(\+\d{1,3}\))?(\d{3}-){2}\d{4}(\/\d{3,4})?$/

Matches:

999-999-9999
(+1)999-999-9999
(+11)999-999-9999
(+111)999-999-9999
999-999-9999/000
(+1)999-999-9999/000
(+11)999-999-9999/000
(+111)999-999-9999/000
999-999-9999/0000
(+1)999-999-9999/0000
(+11)999-999-9999/0000
(+111)999-999-9999/0000

I like to use this tool: http://gskinner.com/RegExr/

It implements Flash's version of RegEx, which is not perfectly identical to Javascript. But it's close enough for most work. If someone else can suggest a JS RegEx tool, even better.

ArrayKnight
  • 5,418
  • 4
  • 15
  • 20