To illustrate the subtle problem, here are some examples in JavaScript so you can test right in your browser:
/[2-5]+/.test('2') // true, as expected.
/[2-5]+/.test('-') // false, as expected.
/[2-5]+/.test('2-') // true. WTF?!!!
Questions:
- Is this a bug or a feature?
- How do you exclude the hyphen when you test a character class?
Update
This is a stupid question. My bad. Need to get some rest before coding again.
Try something like this.
This will make sure that there are only the numbers 2-5 from the front (^) to the end ($) of the string.