-1

I have a regex problem with validation for a region code. My region code could be only one digit but it also could be a digits separated by '-'

for Example my region code could be one of the following:
6
6-66
77-7

As you can see I must have at least one digit or digits separated by '-' and if they are separated there should be a digits after the '-' sign (does not matter how many). So 6- must not be validated as legal region code. I try 2 hours to solve this, but I couldn't, so please help me! Thank you!

1
  • 2
    Show us some of your attempts. This is not the most elaborate of regex tasks. Have you checked out a good tutorial? Commented Jun 14, 2013 at 21:49

4 Answers 4

2
/\d+(-\d+)?$/

This will match 6, 6-66,77-7, but not6-`

2
  • 10x. Can you explain what exactly ?$ do after the group() Commented Jun 15, 2013 at 20:35
  • @KaloyanStamatov The ? makes the group optional. The $ anchors it to the end of the statement (i.e. \d+ must be the end, or -\d+ must be the end of it's there). This prevents additional characters at the end. You could also add ^ at the start to be even more specific Commented Jun 16, 2013 at 13:56
1

If what you are looking for is the whole string:

/^\d+(?:-\d+)?$/

or something like that:

if (parseInt(yourstring.split(/-/)[0])>=eval(yourstring)) alert('true');
else alert('false');

But it is more complicated :) and less efficient! And if the condition is false you code will crash!

2
  • +1, but you probably also want to add anchors. This is about validating a string, not just finding a substring. Commented Jun 14, 2013 at 21:51
  • /\d+(?:-\d+)?/.test('6-') "true" Commented Jun 14, 2013 at 21:52
0
var data = ['6', '6-66', '77-7', '6-'];

var len = data.length;

for(var i=0; i<len; ++i) {
  var current = data[i];
  var result = data[i].match(/^(\d+|\d+[-]\d+)$/);
  if(result != null) {
    console.log(current);
  }
}

--output:--
6
6-66
77-7
0

For a quick answer you can try following:

 /^([0-9])|([0-9]\-[0-9][0-9])|([0-9][0-9]\-[0-9])$/

or in case your engine support perl-styled character classes:

 /^(\d)|(\d\-\d\d)|(\d\d\-\d)$/

here what it does:

  1. between / and / resides as string defining a regular expression

  2. \d stands for one digit it coudl also be writen as [0-9]

  3. () defines a sub-expression, so (\d) matches your first one-digit, (\d-\d\d) second three digits style, and last (\d\d-\d) third variant of three-digit region code

  4. | goes as "OR" like (A)|(B)|(C), so by combining previous three we will get:

    /(\d)|(\d-\d\d)|(\d\d-\d)/

  5. Finally ^ means start of string, and $ - end of string.

also there is so called BRE mode (in which you have to add "\" symbol before each parentheses), but I think it is not the case. However if you would have some free time, please consider any quick tutorial like this one.

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.