Regex Tools |
grep |
PowerGREP |
RegexBuddy |
RegexMagic |
General Applications |
EditPad Lite |
EditPad Pro |
Databases |
MySQL |
Oracle |
PostgreSQL |
XRegExp is an open source JavaScript library developed by Steven Levithan. It supports all modern browsers, as well as many older and even ancient browser versions. It can also be used on the server with Node.js. You can download XRegExp at xregexp.com.
Using the XRegExp object instead of JavaScript’s built-in RegExp object provides you with an regular expression syntax with more features and fewer cross-browser inconsistencies. Notable added features include free-spacing, named capture, mode modifiers, and Unicode categories, blocks, and scripts. It also treats invalid escapes and non-existent backreferences as errors.
XRegExp also provides its own replace() method with a replacement text syntax that is enhanced with named backreferences and no cross-browser inconsistencies. It also provides a split() method that is fully compliant with the JavaScript standard.
To use XRegExp, first create a regular expression object with var myre = XRegExp('regex', 'flags') where flags is a combination of the letters g (global), i (case insensitive), m (anchors match at line breaks), s (dot matches line breaks), x (free-spacing), and n (explicit capture). XRegExp 3 adds the A (astral) flag which includes Unicode characters beyond U+FFFF when matching Unicode properties and blocks. The ECMAScript 6 flags y (sticky) and u (Unicode) can also be used in modern browsers that support them natively, but they’ll throw errors in browsers that don’t have built-in support for these flags.
You can then pass the XRegExp instance you constructed to various XRegExp methods. It’s important to make the calls as shown below to get the full XRegExp functionality. The object returned by the XRegExp constructor is a native JavaScript RegExp object. That object’s methods are the browser’s built-in RegExp methods. You can replace the built-in RegExp methods with XRegExp’s methods by calling XRegExp.install('natives'). Doing so also affects RegExp objects constructed by the normal RegExp constructor or double-slashed regex literals.
XRegExp.test(str, regex, [pos=0], [sticky=false]) tests whether the regex can match part of a string. The pos argument is a zero-based index in the string where the match attempt should begin. If you pass true or 'sticky' for the sticky parameter, then the match is only attempted at pos. This is similar to adding the start-of-attempt anchor \G (which XRegExp doesn’t support) to the start of your regex in other flavors.
XRegExp.exec(str, regex, [pos=0], [sticky=false]) does the same as XRegExp.test() but returns null or an array instead of false or true. Index 0 in the array holds the overall regex match. Indexes 1 and beyond hold the text matched by capturing groups, if any. If the regex has named capturing groups then their matches are available as properties on the array in XRegExp 4 and prior. In XRegExp 5 the array has a group property which then has the names of the capturing groups as properties. XRegExp.exec() does not rely on the lastIndex property and thus avoids cross-browser problems with that property.
XRegExp.forEach(str, regex, callback) makes it easy to iterate over all matches of the regex in a string. It always iterates over all matches, regardless of the global flag an the lastIndex property. The callback is called with four arguments. The first two are an array like returned by exec() and the index in the string that the match starts at. The last two are str and regex exactly as you passed them to forEach().
XRegExp.replace(str, regex, replacement, [scope]) returns a string with the matches of regex in str replaced with replacement. Pass 'one' or 'all' as the scope argument to replace only the first match or all matches. If you omit the scope argument then the regex.global flag determines whether only the first or all matches are replaced.
The XRegExp.replace() method uses its own replacement text syntax. It is very similar to the native JavaScript syntax. It is somewhat incompatible by making dollar signs that don’t form valid replacement tokens an error. But the benefit is that it eliminates all cross-browser inconsistencies. $$ inserts a single literal dollar sign. $& and $0 insert the overall regex match. $` and $' insert the part of the subject string to the left and the right of the regex match. $n, $nn, ${n}, and ${nn} are numbered backreferences while ${name} is a named backreference.
If you pass a function as the replacement parameter, then it will be called with three or more arguments. The first argument is the string that was matched, with named capturing groups available through properties on that string. The second and following arguments are the strings matched by each of the capturing groups in the regex, if any. The final two arguments are the index in the string at which the match was found and the original subject string.
XRegExp.split(str, regex, [limit]) is an alternative to String.prototype.split. It accurately follows the JavaScript standard for splitting strings, eliminating all cross-browser inconsistencies and bugs.
| Quick Start | Tutorial | Tools & Languages | Examples | Reference | Book Reviews |
| grep | PowerGREP | RegexBuddy | RegexMagic |
| EditPad Lite | EditPad Pro |
| Boost | Delphi | GNU (Linux) | Groovy | Java | JavaScript | .NET | PCRE (C/C++) | PCRE2 (C/C++) | Perl | PHP | POSIX | PowerShell | Python | R | Ruby | std::regex | Tcl | VBScript | Visual Basic 6 | wxWidgets | XML Schema | Xojo | XQuery & XPath | XRegExp |
| MySQL | Oracle | PostgreSQL |
Page URL: https://www.regular-expressions.info/xregexp.html
Page last updated: 5 November 2021
Site last updated: 29 August 2024
Copyright © 2003-2024 Jan Goyvaerts. All rights reserved.