What is xregexp?
The xregexp npm package is an extended JavaScript regex library that provides additional syntax and features on top of the standard JavaScript RegExp object. It allows for more readable and maintainable regular expressions, adds new regex syntax, and provides utility functions for working with regular expressions.
What are xregexp's main functionalities?
Extended Syntax
Allows the use of Unicode property escapes for matching characters of a given Unicode category, such as letters (\p{L}).
/\p{L}+/u
Named Capture Groups
Supports named capture groups, which can be accessed by name after a match, making the regex more readable and maintainable.
XRegExp('(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})')
Add-ons and Extensibility
Provides a plugin-style architecture for adding new features like namespacing, which allows using regex flags in a scoped manner.
XRegExp.install('namespacing'); XRegExp('\p{Greek}+', 'A');
Build Regexes from Smaller Parts
Enables the construction of complex regexes from smaller, reusable components, improving modularity and readability.
XRegExp.build('(?x)^ {{integer}} (\. {{integer}})? $', {integer: /\d+/})
Utility Functions
Includes utility functions like matchRecursive for matching balanced pairs of delimiters, which is not possible with native JavaScript regexes.
XRegExp.matchRecursive('test (123 (test) 456) test', '\(', '\)', 'g')
Other packages similar to xregexp
regexp2
regexp2 is a regular expression library that aims to bring all the features from different regex implementations to JavaScript. It is similar to xregexp in providing additional regex syntax and features.
re2
re2 is a fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, and Python. It is different from xregexp in that it focuses on performance and safety rather than extended syntax.
This is XRegExp in a Common JS module. All plugins and
unicode packages are included as well. I'm not affiliated with Steven Levithan,
the author of XRegExp.
Installation
npm install xregexp
Usage example
For JavaScripters:
var XRegExp = require('xregexp');
console.log('Héllö Wôrld'.match(XRegExp('\\p{L}+')));
var date = XRegExp('(?<year> [0-9]{4}) -? # year \n\
(?<month> [0-9]{2}) -? # month \n\
(?<day> [0-9]{2}) # day ', 'x');
var match = date.exec('2011-06-30');
if (match) {
console.log(match.day);
}
For CoffeeScripters:
XRegExp = require 'xregexp'
console.log 'Héllö Wôrld'.match XRegExp '\\p{L}+'
var date = XRegExp '''
(?<year> [0-9]{4}) -? # year
(?<month> [0-9]{2}) -? # month
(?<day> [0-9]{2}) # day
''', 'x'
match = date.exec '2011-06-30'
console.log match.day if match
License
This project includes the software packages:
- XRegExp 1.5.0
- XRegExp Unicode plugin base 0.5
- XRegExp Unicode plugin pack: Categories 1.0
- XRegExp Unicode plugin pack: Scripts 1.0
- XRegExp Unicode plugin pack: Blocks 1.0
- XRegExp Match Recursive plugin 0.1.1
The software is released under the MIT license:
Copyright (c) 2007-2010 Steven Levithan <http://xregexp.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.