W3docs

JavaScript Regex Sets and Ranges

Regular expressions (regex) in JavaScript are a powerful tool for text processing and manipulation. Understanding sets and ranges within regex can significantly

Regular expressions (regex) in JavaScript are a powerful tool for text processing and manipulation. A character set (or character class) lets you say "match any one of these characters" at a single position in the string — a building block you reach for constantly when validating input, parsing text, or cleaning data.

This guide covers sets [...], ranges like [a-z0-9], negation [^...], how to escape special characters inside a set, and how sets combine with shorthand classes such as \w and \d. Each example is runnable, so you can confirm the output yourself.

Introduction to Sets in Regex

A set in a regular expression specifies a group of characters, any one of which may match at a given position in the search string. Sets are written inside square brackets []. Crucially, a set matches exactly one character[abc] matches a single a, b, or c, not the string "abc".

Basic Sets

For example, the set [abc] will match any single character that is a, b, or c. With the global flag g, match() returns every such character it finds:

javascript— editable

Negated Sets

To create a negated set that matches any character not listed, put a caret ^ immediately after the opening bracket. For example, [^abc] matches any single character except a, b, or c. The ^ only has this special meaning in the first position; anywhere else it is a literal caret.

javascript— editable

Understanding Ranges

Inside a set you can write a range with a hyphen — start-end — instead of listing every character. A range matches any character whose code point falls between the two endpoints, inclusive. This keeps patterns short and readable.

Numeric Ranges

For instance, [0-9] represents any digit from 0 to 9. Adding + makes it match one or more digits in a row, so consecutive digits are grouped into a single match:

javascript— editable

Alphabetical Ranges

Similarly, [a-z] matches any lowercase letter and [A-Z] any uppercase letter. You can place several ranges in one set — [A-Za-z0-9] covers letters and digits — to match multiple kinds of characters at once:

javascript— editable

A common real-world use is a basic identifier check, where the first character must be a letter and the rest may be letters, digits, or underscores:

javascript— editable

Advanced Use of Sets and Ranges

You can drop shorthand classes straight into a set alongside literal characters and ranges. The shorthands behave the same inside [...] as they do outside:

  • \d — a digit, equivalent to [0-9]
  • \w — a "word" character: [A-Za-z0-9_]
  • \s — whitespace (space, tab, newline, …)

So [\d.] means "a digit or a dot", and [\w-] means "a word character or a hyphen". See Character classes for the full list and the uppercase negated forms (\D, \W, \S).

Example: Combining Word Characters and Special Symbols

Here is a practical case: keep word characters and one specific punctuation mark, while ignoring everything else.

javascript— editable

Here, \w covers all letters, digits, and the underscore. Adding ! to the set also matches the exclamation mark, which \w does not include. This is handy when you want to allow a few specific symbols without opening the match up to all punctuation.

Unicode and Multilanguage Support

Ranges like [a-z] only cover ASCII letters — they miss accented and non-Latin characters. To match letters across every language, use Unicode property escapes (ECMAScript 2018+) together with the u flag. For example, \p{L} matches any kind of letter from any script:

javascript— editable

The u flag is required for \p{...} to work. Learn more in The unicode flag "u" and class \p{...} and the overview of Patterns and flags.

Excluding Ranges in Regular Expressions

Negation also works with ranges. [^0-9] matches any non-digit, and you can mix ranges and individual characters after the caret. A frequent task is stripping or finding everything that is not in an allowed group — here, every character that is not a vowel (the i flag makes it case-insensitive):

Example of Excluding Ranges

javascript— editable

This regex will find all non-vowel characters, including punctuation and spaces. It's a powerful way to filter out unwanted characters from a string.

Escaping Special Characters in Sets

Inside a set, far fewer characters are special than outside one. You only need to worry about four: the closing bracket ], the backslash \, the caret ^ (special only at the start), and the hyphen - (special only between two characters). Escape any of them with a backslash to use them literally.

A useful shortcut: a hyphen is treated as a literal if it is the first or last character in the set, so [-+] and [a-z-] need no escape. Likewise, characters that are special outside a set — such as ., *, +, (, ) — lose their meaning inside one and match literally, so [.+] simply matches a dot or a plus.

Example of Escaping Special Characters

javascript— editable

Here the brackets are escaped so they are read as literal characters instead of opening and closing a set. The next example shows the hyphen-as-literal rule and dot-loses-its-power rule together:

javascript— editable

Conclusion

Mastering sets and ranges in JavaScript regex not only enhances your string manipulation capabilities but also leads to cleaner, more efficient code. They are particularly powerful for parsing text, validating input, and processing data in web development.

Practice

Practice
What are the characteristics and functionalities of JavaScript Sets and Ranges?
What are the characteristics and functionalities of JavaScript Sets and Ranges?
Was this page helpful?