1

Possible Duplicate:
how to match a number which is less than or equal to 100?

i need a regular expression between these two values 1000 <= x <= 500000 im trying with this one that i constructed but doesnt seem to work

/(1[8-9]|[8-9]|[8-9]|5[0-9]|[0-9]|[0-9]|[0-9]|[0-9])/

Any ideas? thanks in advance!

Community
  • 1
  • 1
Solar Confinement
  • 2,153
  • 3
  • 21
  • 28

3 Answers3

4
\b([1-9][0-9]{3,4}|[1-4][0-9]{5}|500000)\b
aquinas
  • 23,318
  • 5
  • 58
  • 81
4

Is there a particular reason you don't just test the numbers as numbers?

var yourNum = parseInt(yourString, 10); // use parseFloat if it has decimals
if (yourNum >= 1000 && yourNum <= 500000) {
    // success
} else 
    // fail
}
jbabey
  • 45,965
  • 12
  • 71
  • 94
1

Match the cases 1000-9999, 10000-99999, 100000-499999 or 500000:

([1-9]\d{3}|[1-9]\d{4}|[1-4]\d{5}|500000)

Or combining the two first:

([1-9]\d{3,4}|[1-4]\d{5}|500000)
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer. – Guffa Jan 28 '13 at 14:30
  • Perhaps somebody is jealous of your reputation^.^ – Christoph Jan 28 '13 at 14:40
  • 1
    Friends don't let friends downvote without commenting! – jbabey Jan 28 '13 at 15:02
  • Needs to be anchored with `^` and `$`, or e.g. `/([1-9]\d{3,4}|[1-4]\d{5}|500000)/.test('500000000000000000000000000'); // true` – MikeM Jan 28 '13 at 18:39
  • @MikeM: That depends on what the regular expression is going to be used for. For validation you would naturally have to anchor the pattern, but from the OPs attempt it looks rather like it would be used to capture a value. – Guffa Jan 28 '13 at 19:27