Regenerate
Regenerate is a Unicode-aware regex generator for JavaScript. It allows you to easily generate JavaScript-compatible regular expressions based on a given set of Unicode symbols or code points.
Feel free to fork if you see possible improvements!
Installation
Via npm:
npm install regenerate
Via Bower:
bower install regenerate
Via Component:
component install mathiasbynens/regenerate
In a browser:
<script src="regenerate.js"></script>
In Node.js, and RingoJS ≥ v0.8.0:
var regenerate = require('regenerate');
In Narwhal and RingoJS ≤ v0.7.0:
var regenerate = require('regenerate').regenerate;
In Rhino:
load('regenerate.js');
Using an AMD loader like RequireJS:
require(
{
'paths': {
'regenerate': 'path/to/regenerate'
}
},
['regenerate'],
function(regenerate) {
console.log(regenerate);
}
);
API
regenerate(value1, value2, value3, ...)
The main Regenerate function. Calling this function creates a new set that gets a chainable API.
var set = regenerate()
.addRange(0x60, 0x69)
.remove(0x62, 0x64)
.add(0x1D306)
set.valueOf();
set.toString();
set.toRegExp();
Any arguments passed to regenerate()
will be added to the set right away. Both code points (numbers) as symbols (strings consisting of a single Unicode symbol) are accepted.
regenerate(0x1D306, 'A', '©', 0x2603).toString();
regenerate.prototype.add(value1, value2, value3, ...)
Any arguments passed to add()
are added to the set. Both code points (numbers) as symbols (strings consisting of a single Unicode symbol) are accepted, as well as arrays containing values of these types.
regenerate().add(0x1D306, 'A', '©', 0x2603).toString();
regenerate.prototype.remove(value1, value2, value3, ...)
Any arguments passed to remove()
are removed to the set. Both code points (numbers) as symbols (strings consisting of a single Unicode symbol) are accepted, as well as arrays containing values of these types.
regenerate(0x1D306, 'A', '©', 0x2603).remove('☃').toString();
Functions can also be passed. In that case, the result of calling the function against a code point value in the set determines whether the element should be removed (true
) or not (false
).
regenerate(0x1D306, 'A', '©', 0x2603).remove(function(codePoint) {
return codePoint > 0xFFFF;
}).toString();
regenerate.prototype.addRange(start, end)
Adds a range of code points from start
to end
(inclusive) to the set. Both code points (numbers) as symbols (strings consisting of a single Unicode symbol) are accepted.
regenerate(0x1D306).addRange(0x00, 0xFF).toString(16);
regenerate().addRange('A', 'z').toString();
regenerate.prototype.removeRange(start, end)
Removes a range of code points from start
to end
(inclusive) from the set. Both code points (numbers) as symbols (strings consisting of a single Unicode symbol) are accepted.
regenerate()
.addRange(0x000000, 0x10FFFF)
.removeRange('A', 'z')
.toString();
regenerate()
.addRange(0x000000, 0x10FFFF)
.removeRange(0x0041, 0x007A)
.toString();
regenerate.prototype.difference(codePoints)
Removes any code points from the set that are present in both the set and the given codePoints
array. codePoints
must be an array of numeric code point values, i.e. numbers. If you want to use symbol values (strings) as well, use regenerate#remove()
instead.
regenerate()
.addRange(0x00, 0xFF)
.difference([0x61, 0x73])
.toString();
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.
regenerate()
.addRange(0x00, 0xFF)
.intersection([0x61, 0x69])
.toString();
regenerate.prototype.contains(value)
Returns true
if the given value is part of the set, and false
otherwise. Both code points (numbers) as symbols (strings consisting of a single Unicode symbol) are accepted.
var set = regenerate().addRange(0x00, 0xFF);
set.contains('A');
set.contains(0x1D306);
regenerate.prototype.toString()
Returns a string representing (part of) a regular expression that matches all the symbols mapped to the code points within the set.
regenerate(0x1D306, 0x1F4A9).toString();
regenerate.prototype.toRegExp()
Returns a regular expression that matches all the symbols mapped to the code points within the set.
var regex = regenerate(0x1D306, 0x1F4A9).toRegExp();
regex.test('𝌆');
regex.test('A');
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(0x1D306)
.addRange(0x60, 0x65)
.add(0x59, 0x60)
.valueOf();
regenerate.version
A string representing the semantic version number.
regenerate.fromCodePoints(codePoints)
This function takes an array of numerical code point values and returns a string representing (part of) a regular expression that would match all the symbols mapped to those code points.
regenerate.fromCodePoints([0x1F604, 0x1F605, 0x1F606, 0x1F607]);
regenerate.fromCodePointRange(start, end)
This function takes a start
and an end
code point value, and returns a string representing (part of) a regular expression that would match all the symbols mapped to the code points within the range [start, end] (inclusive).
regenerate.fromCodePointRange(0x1F604, 0x1F607);
regenerate.fromCodePointRange(0x000000, 0x10FFFF);
regenerate.fromCodePointRanges(ranges)
This function takes an array of code point ranges or separate code points, and returns a string representing (part of) a regular expression that would match all the symbols mapped to the code points within the listed code points or code point ranges.
regenerate.fromCodePointRanges([
[0x00, 0xFF],
[0x2603, 0x2608],
0x1F4A9,
0x1F4BB
]);
regenerate.fromCodePointRanges([
[0x0000, 0x2602],
[0x2604, 0x1F4A8],
[0x1F4AA, 0x10FFFF]
]);
regenerate.fromSymbols(symbols)
This function takes an array of strings that each contain a single Unicode symbol. It returns a string representing (part of) a regular expression that would match all those symbols.
regenerate.fromSymbols(['𝐀', '𝐁', '𝐂', '𝐃', '𝐄']);
regenerate.fromSymbolRange(start, end)
This function takes a start
and an end
string which each contain a single Unicode symbol. It returns a string representing (part of) a regular expression that would match all the symbols within the range [start, end] (inclusive).
regenerate.fromSymbolRange('𝐏', '𝐟');
regenerate.fromSymbolRanges(ranges)
This function takes an array of symbol ranges or separate strings, each containing a single Unicode symbol, and returns a string representing (part of) a regular expression that would match all the symbols within the listed symbols or symbol ranges.
regenerate.fromSymbolRanges([
['\0', '\xFF'],
['\u2603', '\u2608'],
'\uD83D\uDCA9',
'\uD83D\uDCBB'
]);
regenerate.range(start, end)
This function takes a start
and an end
number and returns an array of numbers progressing from start
up to and including end
, i.e. all the numbers within the range [start, end] (inclusive).
regenerate.range(0x00, 0xFF);
regenerate.ranges(ranges)
This function takes an array of code point ranges or separate code points, and returns an array containing all the code points within the listed code points or code point ranges.
var codePoints = regenerate.ranges([
[0x00, 0xFF],
[0x2603, 0x2608],
0x1F4A9,
0x1F4BB
]);
regenerate.fromCodePoints(codePoints);
regenerate.contains(array, value)
Returns true
if array
contains value
, and false
otherwise.
var ASCII = regenerate.range(0x00, 0xFF);
regenerate.contains(ASCII, 0x61);
regenerate.contains(ASCII, 0x1D306);
regenerate.difference(array1, array2)
Returns an array of array1
elements that are not present in array2
.
regenerate.difference(
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06],
[0x01, 0x03, 0x05]
);
regenerate.intersection(array1, array2)
Returns an array of unique elements that are present in both array1
and array2
.
regenerate.intersection(
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06],
[0x01, 0x03, 0x05, 0x07]
);
regenerate.add(array, value)
Extends array
based on value
as follows:
- If
value
is a code point (i.e. a number), it’s appended to array
. - If
value
is a symbol (i.e. a string containing a single Unicode symbol), its numeric code point value is appended to array
. - If
value
is an array, all its values are added to array
following the above steps.
regenerate.add(
[0x00, 0x1D306],
0x41
);
regenerate.add(
[0x00, 0x1D306],
'A'
);
regenerate.add(
[0x00, 0x1D306],
[0x61, 0x203B, 'A']
);
regenerate.remove(array, value)
Removes values from array
based on value
as follows:
- If
value
is a code point (i.e. a number), it’s removed from array
. - If
value
is a symbol (i.e. a string containing a single Unicode symbol), its numeric code point value is removed from array
. - If
value
is an array, all its values are removed from array
following on the above steps.
regenerate.remove(
[0x00, 0x1D306, 0x41],
0x41
);
regenerate.remove(
[0x00, 0x1D306, 0x41],
'A'
);
regenerate.remove(
[0x00, 0x1D306, 0x61, 0x203B, 0x41],
[0x61, 0x203B, 'A']
);
Combine Regenerate with other libraries
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:
var regenerate = require('regenerate');
var punycode = require('punycode');
var string = 'Lorem ipsum dolor sit amet.';
var codePoints = punycode.ucs2.decode(string);
regenerate(codePoints).toString();
Support
Regenerate has been tested in at least Chrome 27-29, Firefox 3-22, Safari 4-6, Opera 10-12, IE 6-10, Node.js v0.10.0, Narwhal 0.3.2, RingoJS 0.8-0.9, PhantomJS 1.9.0, and Rhino 1.7RC4.
Unit tests & code coverage
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
.
Author
License
Regenerate is available under the MIT license.