Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A regular-expression character set matches one character chosen from a group—not the whole group as a unit. In JavaScript, for example, [aeiou] matches one listed lowercase ASCII vowel. Add a quantifier such as + when you want consecutive matches. This tutorial uses JavaScript regex syntax as its baseline and calls out where Python behaves differently.

What a character set matches

Square brackets define a character class, also called a character set. At the current match position, [abc] accepts one character: a, b, or c. It does not match the three-character string abc as a unit. The MDN Web Docs JavaScript guide describes character classes as distinguishing kinds of characters, such as letters from digits: Character classes.

Start with a literal choice: in JavaScript, [aeiou] matches one lowercase ASCII vowel. The listed characters are alternatives for that one position, not a word or sequence.

Build sets with ranges and repetition

Use a range for a consecutive run

In JavaScript, [0-9] matches one ASCII digit from 0 through 9. A hyphen between endpoints denotes the range. Keep ranges straightforward and check the syntax of your target regex engine before relying on more complicated forms.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Mastering Regular Expressions
  • Used Book in Good Condition

Combine ranges and literals

In JavaScript, [A-F0-9] matches one uppercase letter from A through F or one ASCII digit. A class can combine individual characters and ranges.

Add a quantifier for multiple characters

In JavaScript, [0-9]+ matches one or more consecutive ASCII digits. The class still defines which single character is allowed at each position; + repeats that choice. Likewise, JavaScript /[A-F0-9]+/ finds a run of one or more hexadecimal-looking ASCII characters, but it does not by itself establish that the entire input is a valid hexadecimal value. A whole-input check needs anchors or another validation strategy appropriate to the surrounding code.

Exclude characters with a negated class

A caret immediately after the opening bracket negates the class. In JavaScript, [^,;] matches one character other than a comma or semicolon at the current position. Negation applies to that one character; it does not automatically consume a whole field or line. Add a quantifier if you need a run of characters that excludes those delimiters, and define how the run should end.

Use shorthand classes with the engine in mind

Shorthands provide convenient predefined classes, but their definitions depend on regex flavor and mode. The following comparison describes JavaScript and Python behavior documented by their respective references:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Shorthand JavaScript Python
d Equivalent to [0-9] in the documented JavaScript behavior. For Unicode str patterns, the definition may include Unicode decimal digits; ASCII mode restricts it to ASCII digits.
w Uses ASCII letters, digits, and underscore in the ordinary documented behavior; Unicode-aware case-folding can affect it with particular flags. For Unicode str patterns, includes Unicode alphanumeric characters plus underscore. ASCII mode makes it equivalent to [a-zA-Z0-9_].
s A predefined whitespace class; consult the JavaScript character-class escape reference for the exact behavior. Behavior is defined by Python’s re rules and can differ from JavaScript; consult the documentation for the mode and pattern type in use.

References: MDN JavaScript character-class escapes and the Python 3.14.8 re documentation. In particular, do not assume that w means every letter used in every language: Python Unicode string patterns and ordinary JavaScript behavior have different boundaries.

Escape special characters carefully

Some characters have structural roles inside a class. In JavaScript, ] closes the class, - can define a range, and a leading ^ negates the class. A backslash introduces an escape. If you need one of these characters literally, escape it or place it where the target engine unambiguously treats it as literal. For example, JavaScript []] represents a class containing a literal closing bracket. Escaping rules can vary across flavors, so check the documentation for the engine you are using.

There may also be two parsers involved: the regex parser and the programming language’s string-literal parser. In Python source, for example, a string literal processes backslashes before the regex engine receives the pattern. Python’s documentation recommends raw string notation in many regex examples to avoid that extra layer; r"[]]" expresses the pattern without treating the backslash as a Python string escape first. Raw strings do not change regex syntax—they only affect how Python reads the source string.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Match Unicode character properties when needed

If the requirement is based on a character property—such as matching letters rather than a short, explicitly listed set—use a Unicode-aware feature supported by your target engine. JavaScript documents p{...} and P{...} property escapes in the appropriate mode. Their property names and availability are not universal across regex implementations, so verify the engine and flags before adopting them. The Unicode Consortium’s UTS #18, revision 21 describes broader character-class capabilities; support for every capability described there should not be assumed in a particular engine.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

SaleBestseller No. 1
Mastering Regular Expressions
Mastering Regular Expressions
Used Book in Good Condition
$25.48
SaleBestseller No. 3
Bestseller No. 4
SaleBestseller No. 5

Choose the right kind of set

  • Use explicit members when the allowed characters are a small, exact list, as in JavaScript [aeiou].
  • Use a range when the intended characters form a simple consecutive sequence, such as JavaScript [0-9].
  • Use a shorthand when its documented definition matches your requirement and target engine, such as JavaScript’s ASCII digit class.
  • Use a Unicode property or explicit Unicode-aware definition when the requirement concerns letters or other character categories beyond a narrow ASCII set.
  • Check portability if the same pattern must run in multiple engines: compare their syntax, flags, and definitions rather than assuming a shorthand or property escape is identical.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.