What is regenerate?
The 'regenerate' npm package is a tool for creating sets of Unicode symbols and generating regular expressions to match them. It is particularly useful for working with Unicode properties and ranges, making it easier to handle complex character sets in a concise and readable manner.
What are regenerate's main functionalities?
Creating a set of Unicode symbols
This feature allows you to create a set of Unicode symbols by adding individual characters or ranges of characters. In this example, a set containing all uppercase and lowercase English letters is created.
const regenerate = require('regenerate');
const set = regenerate().addRange(0x0041, 0x005A).addRange(0x0061, 0x007A);
console.log(set.toString()); // Output: '[A-Za-z]'
Generating regular expressions
This feature allows you to generate a regular expression from the set of Unicode symbols. The example demonstrates creating a regular expression that matches any uppercase or lowercase English letter.
const regenerate = require('regenerate');
const set = regenerate().addRange(0x0041, 0x005A).addRange(0x0061, 0x007A);
const regex = set.toRegExp();
console.log(regex); // Output: /[A-Za-z]/
Adding individual characters
This feature allows you to add individual Unicode characters to the set. In this example, the set contains the characters 'A' and 'a'.
const regenerate = require('regenerate');
const set = regenerate().add(0x0041).add(0x0061);
console.log(set.toString()); // Output: '[Aa]'
Removing characters or ranges
This feature allows you to remove individual characters or ranges of characters from the set. In this example, the character 'A' is removed from a set of uppercase English letters.
const regenerate = require('regenerate');
const set = regenerate().addRange(0x0041, 0x005A).remove(0x0041);
console.log(set.toString()); // Output: '[B-Z]'
Other packages similar to regenerate
xregexp
XRegExp provides augmented (extended) regular expressions. It includes support for Unicode properties, named capture groups, and other features not found in native JavaScript regular expressions. Compared to 'regenerate', XRegExp offers a broader range of regex enhancements but may be more complex to use for simple Unicode set operations.
unicode-regex
The 'unicode-regex' package provides utilities for generating regular expressions that match Unicode characters and properties. It is similar to 'regenerate' but focuses more on providing pre-defined sets for common Unicode properties and categories.
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 and usage
In a browser:
<script src="regenerate.js"></script>
Via npm:
npm install regenerate
In Narwhal, Node.js, and RingoJS:
var regenerate = require('regenerate');
In Rhino:
load('regenerate.js');
Using an AMD loader like RequireJS:
require(
{
'paths': {
'regenerate': 'path/to/regenerate'
}
},
['regenerate'],
function(regenerate) {
console.log(regenerate);
}
);
Usage example:
regenerate.fromCodePoints([0x1F604, 0x1F605, 0x1F606, 0x1F607]);
regenerate.fromCodePointRange(0x1F604, 0x1F607);
regenerate.fromCodePointRange(0x000000, 0x10FFFF);
regenerate.fromSymbols(['𝐀', '𝐁', '𝐂', '𝐃', '𝐄']);
regenerate.fromSymbolRange('𝐏', '𝐟');
Note that all of Regenerate’s methods return strings that can be used as (part of) a regular expression literal. To convert an output string into a regular expression dynamically, just wrap it in RegExp(…)
:
var result = regenerate.fromCodePointRange(0x1F604, 0x1F607);
var regex = RegExp(result);
regex.test('\uD83D\uDE03');
regex.test('\uD83D\uDE04');
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 dual licensed under the MIT and GPL licenses.