Gathering detailed insights and metrics for regexpu-core
Gathering detailed insights and metrics for regexpu-core
Gathering detailed insights and metrics for regexpu-core
Gathering detailed insights and metrics for regexpu-core
regexpu’s core functionality, i.e. `rewritePattern(pattern, flag, options)`, which enables rewriting regular expressions that make use of the ES6 `u` flag into equivalent ES5-compatible regular expression patterns.
npm install regexpu-core
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
71 Stars
173 Commits
12 Forks
9 Watching
2 Branches
7 Contributors
Updated on 21 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-22.8%
4,285,543
Compared to previous day
Last week
-3.3%
29,097,419
Compared to previous week
Last month
11%
120,740,539
Compared to previous month
Last year
1.2%
1,208,711,460
Compared to previous year
regexpu is a source code transpiler that enables the use of ES2015 Unicode regular expressions in JavaScript-of-today (ES5).
regexpu-core contains regexpu’s core functionality, i.e. rewritePattern(pattern, flag)
, which enables rewriting regular expressions that make use of the ES2015 u
flag into equivalent ES5-compatible regular expression patterns.
To use regexpu-core programmatically, install it as a dependency via npm:
1npm install regexpu-core --save
Then, require
it:
1const rewritePattern = require('regexpu-core');
This module exports a single function named rewritePattern
.
rewritePattern(pattern, flags, options)
This function takes a string that represents a regular expression pattern as well as a string representing its flags, and returns an ES5-compatible version of the pattern.
1rewritePattern('foo.bar', 'u', { unicodeFlag: "transform" }); 2// → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar' 3 4rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'u', { unicodeFlag: "transform" }); 5// → '(?:[a-z]|\\uD834[\\uDF06-\\uDF08])' 6 7rewritePattern('[\\u{1D306}-\\u{1D308}a-z]', 'ui', { unicodeFlag: "transform" }); 8// → '(?:[a-z\\u017F\\u212A]|\\uD834[\\uDF06-\\uDF08])'
regexpu-core can rewrite non-ES6 regular expressions too, which is useful to demonstrate how their behavior changes once the u
and i
flags are added:
1// In ES5, the dot operator only matches BMP symbols: 2rewritePattern('foo.bar', '', { unicodeFlag: "transform" }); 3// → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF])bar' 4 5// But with the ES2015 `u` flag, it matches astral symbols too: 6rewritePattern('foo.bar', 'u', { unicodeFlag: "transform" }); 7// → 'foo(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF])bar'
The optional options
argument recognizes the following properties:
These options can be set to false
or 'transform'
. When using 'transform'
, the corresponding features are compiled to older syntax that can run in older browsers. When using false
(the default), they are not compiled and they can be relied upon to compile more modern features.
unicodeFlag
- The u
flag, enabling support for Unicode code point escapes in the form \u{...}
.
1rewritePattern('\\u{ab}', '', { 2 unicodeFlag: 'transform' 3}); 4// → '\\u{ab}' 5 6rewritePattern('\\u{ab}', 'u', { 7 unicodeFlag: 'transform' 8}); 9// → '\\xAB'
dotAllFlag
- The s
(dotAll
) flag.
1rewritePattern('.', '', { 2 dotAllFlag: 'transform' 3}); 4// → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]' 5 6rewritePattern('.', 's', { 7 dotAllFlag: 'transform' 8}); 9// → '[\\0-\\uFFFF]' 10 11rewritePattern('.', 'su', { 12 dotAllFlag: 'transform' 13}); 14// → '(?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])'
unicodePropertyEscapes
- Unicode property escapes.
By default they are compiled to Unicode code point escapes of the form \u{...}
. If the unicodeFlag
option is set to 'transform'
they often result in larger output, although there are cases (such as \p{Lu}
) where it actually decreases the output size.
1rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', { 2 unicodePropertyEscapes: 'transform' 3}); 4// → '[\\u{14400}-\\u{14646}]' 5 6rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', { 7 unicodeFlag: 'transform', 8 unicodePropertyEscapes: 'transform' 9}); 10// → '(?:\\uD811[\\uDC00-\\uDE46])'
namedGroups
- Named capture groups.
1rewritePattern('(?<name>.)\\k<name>', '', { 2 namedGroups: 'transform' 3}); 4// → '(.)\1'
unicodeSetsFlag
- The v
(unicodeSets
) flag
1rewritePattern('[\\p{Emoji}&&\\p{ASCII}]', 'v', { 2 unicodeSetsFlag: 'transform' 3}); 4// → '[#\\*0-9]'
By default, patterns with the v
flag are transformed to patterns with the u
flag. If you want to downlevel them more you can set the unicodeFlag: 'transform'
option.
1rewritePattern('[^[a-h]&&[f-z]]', 'v', { 2 unicodeSetsFlag: 'transform' 3}); 4// → '[^f-h]' (to be used with /u)
1rewritePattern('[^[a-h]&&[f-z]]', 'v', { 2 unicodeSetsFlag: 'transform', 3 unicodeFlag: 'transform' 4}); 5// → '(?:(?![f-h])[\s\S])' (to be used without /u)
modifiers
- Inline i
/m
/s
modifiers
1rewritePattern('(?i:[a-z])[a-z]', '', { 2 modifiers: 'transform' 3}); 4// → '(?:[a-zA-Z])([a-z])'
These options can be set to false
, 'parse'
and 'transform'
. When using 'transform'
, the corresponding features are compiled to older syntax that can run in older browsers. When using 'parse'
, they are parsed and left as-is in the output pattern. When using false
(the default), they result in a syntax error if used.
Once these features become stable (when the proposals are accepted as part of ECMAScript), they will be parsed by default and thus 'parse'
will behave like false
.
onNamedGroup
This option is a function that gets called when a named capture group is found. It receives two parameters: the name of the group, and its index.
1rewritePattern('(?<name>.)\\k<name>', '', { 2 onNamedGroup(name, index) { 3 console.log(name, index); 4 // → 'name', 1 5 } 6});
onNewFlags
This option is a function that gets called to pass the flags that the resulting pattern must be interpreted with.
1rewritePattern('abc', 'um', '', { 2 unicodeFlag: 'transform', 3 onNewFlags(flags) { 4 console.log(flags); 5 // → 'm' 6 } 7})
namedGroups: 'transform'
, regexpu-core only takes care of the syntax: you will still need a runtime wrapper around the regular expression to populate the .groups
property of RegExp.prototype.match()
's result. If you are using regexpu-core via Babel, it's handled automatically.On the main
branch, bump the version number in package.json
:
1npm version patch -m 'Release v%s'
Instead of patch
, use minor
or major
as needed.
Note that this produces a Git commit + tag.
Push the release commit and tag:
1git push --follow-tags
Our CI then automatically publishes the new release to npm.
Once the release has been published to npm, update regexpu
to make use of it, and cut a new release of regexpu
as well.
Mathias Bynens |
regexpu-core is available under the MIT license.
No vulnerabilities found.
Reason
18 commit(s) and 8 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 25/30 approved changesets -- score normalized to 8
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More