Defining ranges in Regex

Ranges in Regex allow you to create your own custom constraints, much like the ones we just went through. In a range, you can specify exactly the characters that can be used or if it's faster, you can specify the inverse, that is, the characters that do not match.

For the sake of illustration, let's say we wanted to match only abc. In this case, we could create a range similar to [abc] and it will match a single character, which is either a, b, or c. Let's test it out with the bicycle text and the /[abc]/g pattern:

Defining ranges in Regex

Defining a range

Now, this will work, however, if you have a lot of characters you need to match, your range will become long quickly. Luckily, Regex allows you to use the (-) dash character to specify a set of characters without needing to list them out. For example, let's say we want to check whether a three lettered name is formatted correctly, and we want the first letter to be a capital letter, followed by two lower case letters. Instead of specifying all 26 letters in each range, we can abbreviate it to [a-z] or [A-Z] for the uppercase letters. So, to implement a three letter name verifier, we could create a pattern similar to/[A-Z][a-z][a-z]/g:

Defining a range

Matching the dash character

If you are trying to match the dash character itself, and you don't want JavaScript to interpret it as specifying a set, you can either start/end the range with the dash character or escape it with a backslash. For example to match both "hello world" and "hello-world," we could write a pattern similar to /hello[- ]world/ or /hello[- ]world/.

We can also use a wild character as a simple dot inside a rage. For example, this may occur when we want to match a number character and we don't mind having a period (forgetting for a second that a number can only have one period). So, to match 123 as well as 2.4 and .45, we could specify the /[d.][d.]d/ pattern, and then both the first and second digits can be periods. Notice, JavaScript doesn't think that we are referring to the wildcard period inside the range, as this would defeat the purpose of a range, so JavaScript treats it as a standard period.

Defining negated ranges

The last thing to be covered in ranges is the negated range. A negated range is exactly what it sounds like. Instead of specifying what to match, we are specifying what not to match. It's very similar to adding a not (!) character to a Boolean value in JavaScript, in that it simply flips the return value of what you would have got earlier.

To create a negated range, you can start the range with a (^) caret character to match any character; however, for the first five letters of the alphabet, you would use something similar to /[^a-e]/.

This may not seem that useful by itself, but you might, for example, want to strip out all not alphabetical characters for a filename. In this case, you can type /[^a-z]/gi and combined with JavaScript's replace function, you can remove all of them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset