Gathering detailed insights and metrics for regenerate
Gathering detailed insights and metrics for regenerate
Gathering detailed insights and metrics for regenerate
Gathering detailed insights and metrics for regenerate
regenerate-unicode-properties
Regenerate sets for Unicode properties and values.
@types/regenerate
TypeScript definitions for regenerate
regexp-class-to-regenerate
convert Specified type RegExp to hacked regenerate object
js-regenerate
Tool to regenerate JavaScript code using esprima and escodegen
Generate JavaScript-compatible regular expressions based on a given set of Unicode symbols or code points.
npm install regenerate
Typescript
Module System
Node Version
NPM Version
99.8
Supply Chain
100
Quality
76
Maintenance
100
Vulnerability
100
License
JavaScript (99.26%)
HTML (0.74%)
Total Downloads
4,814,955,980
Last Day
3,895,526
Last Week
17,534,849
Last Month
79,888,552
Last Year
1,053,749,699
368 Stars
124 Commits
35 Forks
21 Watching
2 Branches
6 Contributors
Minified
Minified + Gzipped
Latest Version
1.4.2
Package Id
regenerate@1.4.2
Size
12.31 kB
NPM Version
6.14.8
Node Version
14.15.0
Publised On
01 Nov 2020
Cumulative downloads
Total Downloads
Last day
-5.1%
3,895,526
Compared to previous day
Last week
-16.3%
17,534,849
Compared to previous week
Last month
3.6%
79,888,552
Compared to previous month
Last year
7.9%
1,053,749,699
Compared to previous year
7
Regenerate is a Unicode-aware regex generator for JavaScript. It allows you to easily generate ES5-compatible regular expressions based on a given set of Unicode symbols or code points. (This is trickier than you might think, because of how JavaScript deals with astral symbols.)
Via npm:
1npm install regenerate
Via Bower:
1bower install regenerate
In a browser:
1<script src="regenerate.js"></script>
In Node.js, io.js, and RingoJS ≥ v0.8.0:
1var regenerate = require('regenerate');
In Narwhal and RingoJS ≤ v0.7.0:
1var regenerate = require('regenerate').regenerate;
In Rhino:
1load('regenerate.js');
Using an AMD loader like RequireJS:
1require( 2 { 3 'paths': { 4 'regenerate': 'path/to/regenerate' 5 } 6 }, 7 ['regenerate'], 8 function(regenerate) { 9 console.log(regenerate); 10 } 11);
regenerate(value1, value2, value3, ...)
The main Regenerate function. Calling this function creates a new set that gets a chainable API.
1var set = regenerate() 2 .addRange(0x60, 0x69) // add U+0060 to U+0069 3 .remove(0x62, 0x64) // remove U+0062 and U+0064 4 .add(0x1D306); // add U+1D306 5set.valueOf(); 6// → [0x60, 0x61, 0x63, 0x65, 0x66, 0x67, 0x68, 0x69, 0x1D306] 7set.toString(); 8// → '[`ace-i]|\\uD834\\uDF06' 9set.toRegExp(); 10// → /[`ace-i]|\uD834\uDF06/
Any arguments passed to regenerate()
will be added to the set right away. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted, as well as arrays containing values of these types.
1regenerate(0x1D306, 'A', '©', 0x2603).toString(); 2// → '[A\\xA9\\u2603]|\\uD834\\uDF06' 3 4var items = [0x1D306, 'A', '©', 0x2603]; 5regenerate(items).toString(); 6// → '[A\\xA9\\u2603]|\\uD834\\uDF06'
regenerate.prototype.add(value1, value2, value3, ...)
Any arguments passed to add()
are added to the set. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted, as well as arrays containing values of these types.
1regenerate().add(0x1D306, 'A', '©', 0x2603).toString(); 2// → '[A\\xA9\\u2603]|\\uD834\\uDF06' 3 4var items = [0x1D306, 'A', '©', 0x2603]; 5regenerate().add(items).toString(); 6// → '[A\\xA9\\u2603]|\\uD834\\uDF06'
It’s also possible to pass in a Regenerate instance. Doing so adds all code points in that instance to the current set.
1var set = regenerate(0x1D306, 'A'); 2regenerate().add('©', 0x2603).add(set).toString(); 3// → '[A\\xA9\\u2603]|\\uD834\\uDF06'
Note that the initial call to regenerate()
acts like add()
. This allows you to create a new Regenerate instance and add some code points to it in one go:
1regenerate(0x1D306, 'A', '©', 0x2603).toString(); 2// → '[A\\xA9\\u2603]|\\uD834\\uDF06'
regenerate.prototype.remove(value1, value2, value3, ...)
Any arguments passed to remove()
are removed from the set. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted, as well as arrays containing values of these types.
1regenerate(0x1D306, 'A', '©', 0x2603).remove('☃').toString(); 2// → '[A\\xA9]|\\uD834\\uDF06'
It’s also possible to pass in a Regenerate instance. Doing so removes all code points in that instance from the current set.
1var set = regenerate('☃'); 2regenerate(0x1D306, 'A', '©', 0x2603).remove(set).toString(); 3// → '[A\\xA9]|\\uD834\\uDF06'
regenerate.prototype.addRange(start, end)
Adds a range of code points from start
to end
(inclusive) to the set. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted.
1regenerate(0x1D306).addRange(0x00, 0xFF).toString(16); 2// → '[\\0-\\xFF]|\\uD834\\uDF06' 3 4regenerate().addRange('A', 'z').toString(); 5// → '[A-z]'
regenerate.prototype.removeRange(start, end)
Removes a range of code points from start
to end
(inclusive) from the set. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted.
1regenerate() 2 .addRange(0x000000, 0x10FFFF) // add all Unicode code points 3 .removeRange('A', 'z') // remove all symbols from `A` to `z` 4 .toString(); 5// → '[\\0-@\\{-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]' 6 7regenerate() 8 .addRange(0x000000, 0x10FFFF) // add all Unicode code points 9 .removeRange(0x0041, 0x007A) // remove all code points from U+0041 to U+007A 10 .toString(); 11// → '[\\0-@\\{-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]'
regenerate.prototype.intersection(codePoints)
Removes any code points from the set that are not present in both the set and the given codePoints
array. codePoints
must be an array of numeric code point values, i.e. numbers.
1regenerate() 2 .addRange(0x00, 0xFF) // add extended ASCII code points 3 .intersection([0x61, 0x69]) // remove all code points from the set except for these 4 .toString(); 5// → '[ai]'
Instead of the codePoints
array, it’s also possible to pass in a Regenerate instance.
1var whitelist = regenerate(0x61, 0x69); 2 3regenerate() 4 .addRange(0x00, 0xFF) // add extended ASCII code points 5 .intersection(whitelist) // remove all code points from the set except for those in the `whitelist` set 6 .toString(); 7// → '[ai]'
regenerate.prototype.contains(value)
Returns true
if the given value is part of the set, and false
otherwise. Both code points (numbers) and symbols (strings consisting of a single Unicode symbol) are accepted.
1var set = regenerate().addRange(0x00, 0xFF); 2set.contains('A'); 3// → true 4set.contains(0x1D306); 5// → false
regenerate.prototype.clone()
Returns a clone of the current code point set. Any actions performed on the clone won’t mutate the original set.
1var setA = regenerate(0x1D306); 2var setB = setA.clone().add(0x1F4A9); 3setA.toArray(); 4// → [0x1D306] 5setB.toArray(); 6// → [0x1D306, 0x1F4A9]
regenerate.prototype.toString(options)
Returns a string representing (part of) a regular expression that matches all the symbols mapped to the code points within the set.
1regenerate(0x1D306, 0x1F4A9).toString(); 2// → '\\uD834\\uDF06|\\uD83D\\uDCA9'
If the bmpOnly
property of the optional options
object is set to true
, the output matches surrogates individually, regardless of whether they’re lone surrogates or just part of a surrogate pair. This simplifies the output, but it can only be used in case you’re certain the strings it will be used on don’t contain any astral symbols.
1var highSurrogates = regenerate().addRange(0xD800, 0xDBFF); 2highSurrogates.toString(); 3// → '[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])' 4highSurrogates.toString({ 'bmpOnly': true }); 5// → '[\\uD800-\\uDBFF]' 6 7var lowSurrogates = regenerate().addRange(0xDC00, 0xDFFF); 8lowSurrogates.toString(); 9// → '(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]' 10lowSurrogates.toString({ 'bmpOnly': true }); 11// → '[\\uDC00-\\uDFFF]'
Note that lone low surrogates cannot be matched accurately using regular expressions in JavaScript without the use of lookbehind assertions, which aren't yet widely supported. Regenerate’s output makes a best-effort approach but there can be false negatives in this regard.
If the hasUnicodeFlag
property of the optional options
object is set to true
, the output makes use of Unicode code point escapes (\u{…}
) where applicable. This simplifies the output at the cost of compatibility and portability, since it means the output can only be used as a pattern in a regular expression with the ES6 u
flag enabled.
1var set = regenerate().addRange(0x0, 0x10FFFF); 2 3set.toString(); 4// → '[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]'' 5 6set.toString({ 'hasUnicodeFlag': true }); 7// → '[\\0-\\u{10FFFF}]'
regenerate.prototype.toRegExp(flags = '')
Returns a regular expression that matches all the symbols mapped to the code points within the set. Optionally, you can pass flags to be added to the regular expression.
1var regex = regenerate(0x1D306, 0x1F4A9).toRegExp(); 2// → /\uD834\uDF06|\uD83D\uDCA9/ 3regex.test('????'); 4// → true 5regex.test('A'); 6// → false 7 8// With flags: 9var regex = regenerate(0x1D306, 0x1F4A9).toRegExp('g'); 10// → /\uD834\uDF06|\uD83D\uDCA9/g
Note: This probably shouldn’t be used. Regenerate is intended as a tool that is used as part of a build process, not at runtime.
regenerate.prototype.valueOf()
or regenerate.prototype.toArray()
Returns a sorted array of unique code points in the set.
1regenerate(0x1D306) 2 .addRange(0x60, 0x65) 3 .add(0x59, 0x60) // note: 0x59 is added after 0x65, and 0x60 is a duplicate 4 .valueOf(); 5// → [0x59, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x1D306]
regenerate.version
A string representing the semantic version number.
Regenerate gets even better when combined with other libraries such as Punycode.js. Here’s an example where Punycode.js is used to convert a string into an array of code points, that is then passed on to Regenerate:
1var regenerate = require('regenerate'); 2var punycode = require('punycode'); 3 4var string = 'Lorem ipsum dolor sit amet.'; 5// Get an array of all code points used in the string: 6var codePoints = punycode.ucs2.decode(string); 7 8// Generate a regular expression that matches any of the symbols used in the string: 9regenerate(codePoints).toString(); 10// → '[ \\.Ladeilmopr-u]'
In ES6 you can do something similar with Array.from
which uses the string’s iterator to split the given string into an array of strings that each contain a single symbol. regenerate()
accepts both strings and code points, remember?
1var regenerate = require('regenerate'); 2 3var string = 'Lorem ipsum dolor sit amet.'; 4// Get an array of all symbols used in the string: 5var symbols = Array.from(string); 6 7// Generate a regular expression that matches any of the symbols used in the string: 8regenerate(symbols).toString(); 9// → '[ \\.Ladeilmopr-u]'
Regenerate supports at least Chrome 27+, Firefox 3+, Safari 4+, Opera 10+, IE 6+, Node.js v0.10.0+, io.js v1.0.0+, Narwhal 0.3.2+, RingoJS 0.8+, PhantomJS 1.9.0+, and Rhino 1.7RC4+.
After cloning this repository, run npm install
to install the dependencies needed for Regenerate development and testing. You may want to install Istanbul globally using npm install istanbul -g
.
Once that’s done, you can run the unit tests in Node using npm test
or node tests/tests.js
. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use grunt test
.
To generate the code coverage report, use grunt cover
.
Mathias Bynens |
Regenerate is available under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 3/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
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 2025-01-27
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