Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do Unicode letters and digits affect regex matching in Python and JavaScript?

Quick answer

Do not assume that \w, \d, or case-insensitive matching mean the same thing in Python and JavaScript. In Python re, Unicode string patterns use Unicode-aware rules for \w and \d by default, while re.ASCII narrows those shorthand classes. JavaScript documents \w as basic-Latin letters, digits, and underscore, and \d as [0-9]. Choose a specific property or range for your intended text, then test with your actual flags.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a regex using \w and \d. Can I assume those mean any letter and any digit in both Python and JavaScript?

Geez

No. The shorthand classes differ by language, and “letter” or “digit” needs a precise definition for your task.

For Python re patterns on Unicode strings, \d matches Unicode decimal digits, and \w includes Unicode alphanumeric characters plus underscore. The re.ASCII flag narrows those shorthand classes to ASCII behavior. [S1]

In JavaScript, MDN documents \d as [0-9] and \w as basic-Latin letters, digits, and underscore. For a Unicode letter property, JavaScript offers property escapes such as \p{L} with the appropriate Unicode mode. [S2]

So a pattern can accept a character in one language and reject it in the other. First decide what your input should count as a letter or digit; then choose syntax and flags for that language rather than translating shorthand blindly.

Follow-up question

What should I use if a field should allow Unicode letters and decimal digits, but still allow underscore?

Geez

Treat that as a specific requirement, not as “use \w everywhere.” In Python re on a Unicode string, \w includes Unicode alphanumeric characters and underscore; \d matches Unicode decimal digits. If you enable re.ASCII, those shorthand rules become ASCII-only. [S1]

In JavaScript, \w and \d are narrower according to the documented definitions. A Unicode property escape can express a letter property, for example \p{L}, when used with the appropriate Unicode mode. [S2] Don’t infer from that example alone that your complete field rule is satisfied: decide whether you need letters, decimal digits, underscore, or some combination, and construct and test the whole character class accordingly.

A useful planning note is: “Accept [intended letter property], [intended digit property], and underscore; reject everything else.” That keeps the requirement clear even if you later change regex engines.

Follow-up question

Does case-insensitive matching change what counts as a letter?

Geez

It can, so include case behavior in your checks. Python documents Unicode-related behavior for IGNORECASE: [a-z] or [A-Z] can match additional non-ASCII characters unless ASCII mode is used. That behavior is specific to Python re; check the pattern type and flags you actually use. [S1]

For JavaScript, do not assume Python’s case-insensitive rules apply. Check the JavaScript regex documentation for the flags and mode used by your pattern, and test the exact characters relevant to your input. The cited JavaScript character-class guidance establishes the documented shorthand definitions and availability of Unicode property escapes; it does not establish a universal rule for every script, character, or regex engine. [S2]

For example, if your rule is intended to accept only ASCII letters regardless of language, write and test an explicit ASCII range rather than relying on a broad shorthand. If it is intended to accept Unicode letters, state that property explicitly where the engine supports it.

Follow-up question

Can you give me a quick way to check my intended characters before I ship the pattern?

Geez

Make a small test set from the actual requirement. For instance, use placeholders such as [ASCII letter], [non-ASCII letter you intend to accept], [ASCII digit], [non-ASCII decimal digit you intend to accept], [underscore], and [punctuation you intend to reject]. Replace those labels with real test characters in your local test cases; the right characters depend on your application.

For each case, record the expected result, then run the same cases with the exact regex, language version, pattern type, and flags your application uses. Test both whole-string validation and any substring search behavior you rely on. Compare Python and JavaScript results separately; matching behavior in one is not evidence about the other.

This check confirms only the cases you test. It does not prove that a pattern covers every script or every character users may enter. For broader coverage, define the accepted character properties and consult the documentation for the specific language, engine, and active flags.

A practical checklist

  • Write down whether the field allows ASCII only or Unicode letters and digits.

  • Specify whether underscore, punctuation, and whitespace are allowed.

  • Check the language, regex engine, pattern type, and active flags.

  • Test intended and rejected characters individually, including non-ASCII cases relevant to your requirement.

  • Run the test set in each implementation; do not assume Python and JavaScript behave identically.

A few more useful answers

Does Python `\w` mean every Unicode character that is a letter?

No. Python documents \w for Unicode string patterns as Unicode alphanumeric characters plus underscore; that is not a claim that it matches every character someone might describe as a letter. Define and test the exact property your application needs. [S1]

Does JavaScript `\d` match Unicode decimal digits?

MDN documents JavaScript \d as equivalent to [0-9]. If you need a broader digit rule, choose an explicitly suitable Unicode property or other documented pattern and test it in the intended mode. [S2]

Will the same regex behave identically in Python and JavaScript?

Not necessarily: their documented shorthand-class definitions differ, and flags or modes can affect behavior. Verify each implementation against the same expected test cases.

Sources

References used for this guide.

  1. [S1] Python 3.15.2 re — Regular expression operations
    docs.python.org · Checked
  2. [S2] Character classes — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work