oxc-project/oxc · warning · OxcDiagnostic

Use the 'u' flag.

Error message

Use the 'u' flag.

What it means

oxlint `eslint/require-unicode-regexp` (default configuration): a regex was created without the `u` flag. The else-branch of `require_unicode_regexp_diagnostic` (require_unicode_regexp.rs:33) selects 'Use the \'u\' flag.' / 'Add the \'u\' flag.'. Unicode mode makes `.` and quantifiers operate on code points rather than UTF-16 code units and enables `\p{...}` properties, preventing subtle surrogate-pair bugs.

Source

Thrown at crates/oxc_linter/src/rules/eslint/require_unicode_regexp.rs:33

    AstNode,
    ast_util::get_declaration_of_variable,
    ast_util::variable_declaration_kind,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
    utils::is_regexp_callee,
};

fn require_unicode_regexp_diagnostic(
    span: Span,
    require_flag: Option<&RequireFlag>,
) -> OxcDiagnostic {
    let (msg, help_msg) = if matches!(require_flag, Some(RequireFlag::V)) {
        ("Use the 'v' flag.", "Add the 'v' flag.")
    } else {
        ("Use the 'u' flag.", "Add the 'u' flag.")
    };

    OxcDiagnostic::warn(msg)
        .with_note("The 'u' and 'v' flags enable Unicode-aware regular expression behavior and stricter pattern parsing.")
        .with_help(help_msg)
        .with_label(span)
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
struct RequireUnicodeRegexpConfig {
    /// The `u` flag may be preferred in environments that do not support the `v` flag.
    ///
    /// Examples of **incorrect** code for this rule with the `{ "requireFlag": "u" }` option:
    /// ```js
    /// const fooEmpty = /foo/;
    /// const fooEmptyRegexp = new RegExp('foo');
    /// const foo = /foo/v;
    /// const fooRegexp = new RegExp('foo', 'v');
    /// ```
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Add `u` to the literal: `/foo/` becomes `/foo/u`.
  2. Add `u` to constructor flags: `new RegExp("foo", "iu")`.
  3. After adding `u`, re-test: some patterns (unnecessary escapes, `.`-based splits) become errors or behave differently under Unicode mode.
  4. For specific unavoidable cases, disable per line with `// oxlint-disable-next-line require-unicode-regexp`.

Example fix

// before
const emoji = /^.+$/.test(text);

// after
const emoji = /^.+$/u.test(text);
Defensive patterns

Strategy: validation

Validate before calling

# regex literals without u or v
rg -n --pcre2 '/(?!/)[^\n\\r]*/[dgimsy]*['"'"'",; )]' src/

Prevention

When it happens

Trigger: `/foo/`, `/\d+/`, `new RegExp("foo")`, or `new RegExp("foo", "i")` — any regex whose flags string does not include `u` (or `v`).

Common situations: Enabling this rule on a legacy codebase with many pre-`u` regexes; emoji/CJK text handling where surrogate-pair bugs matter; porting tests from another project's config.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/3e1decc14c606aaf. Report an issue: GitHub.