Regenerate
Last updated
Was this helpful?
Last updated
Was this helpful?
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 .)
Via :
Via :
In a browser:
In , , and :
In and :
In :
Using an AMD loader like :
regenerate(value1, value2, value3, ...)
The main Regenerate function. Calling this function creates a new set that gets a chainable API.
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.
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.
It’s also possible to pass in a Regenerate instance. Doing so adds all code points in that instance to the current set.
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:
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.
It’s also possible to pass in a Regenerate instance. Doing so removes all code points in that instance from the current set.
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.
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.
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.
Instead of the codePoints
array, it’s also possible to pass in a Regenerate instance.
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.
regenerate.prototype.clone()
Returns a clone of the current code point set. Any actions performed on the clone won’t mutate the original set.
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.
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.
regenerate.prototype.toRegExp(flags = '')
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.
regenerate.version
A string representing the semantic version number.
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
.
Note that lone low surrogates cannot be matched accurately using regular expressions in JavaScript without the use of , which aren't yet widely supported. Regenerate’s output makes a best-effort approach but .
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 enabled.
Returns a regular expression that matches all the symbols mapped to the code points within the set. Optionally, you can pass to be added to the regular expression.
Regenerate gets even better when combined with other libraries such as . Here’s an example where is used to convert a string into an array of code points, that is then passed on to Regenerate:
In ES6 you can do something similar with which uses to split the given string into an array of strings that each contain a single symbol. accepts both strings and code points, remember?
Regenerate is available under the license.