Quick Start
Tutorial
Search & Replace
Tools & Languages
Examples
Reference
Regex Tools
grep
PowerGREP
RegexBuddy
RegexMagic
General Applications
EditPad Lite
EditPad Pro
Google Docs
Google Sheets
LibreOffice
Notepad++
Languages & Libraries
Boost
C#
Delphi
F#
GNU (Linux)
Groovy
ICU (Unicode)
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
Python.NET and IronPython
R
RE2
Ruby
std::regex
Tcl
TypeScript
VBScript
Visual Basic 6
Visual Basic (.NET)
wxWidgets
XML Schema
XQuery & XPath
Xojo
XRegExp
Databases
Google BigQuery
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
RegexBuddy—The best regex editor and tester for XRegExp developers!

XRegExp Regular Expression Library for JavaScript

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.

In fact, when XRegExp was first released in 2007, those now-ancient browsers were current. None of them fully adhered to the JavaScript standard, resulting in many cross-browser inconsistencies. XRegExp smoothed over many inconsistencies and browser bugs by implementing workarounds and by tightening up the syntax by treating invalid escapes, octal escapes, and non-existent backreferences as errors.

Using the XRegExp object instead of JavaScript’s built-in RegExp object provided JavaScript developers with an extended regular expression syntax with more features. Notable added features include free-spacing, named capture, mode modifiers, and Unicode categories, blocks, and scripts. XRegExp also provides its own replace() method with a replacement text syntax that is enhanced with named backreferences and a split() method that is fully compliant with the JavaScript standard.

Today, all major browsers correctly implement regular expressions as defined in the ECMAScript standard, eliminating all cross-browser inconsistencies. Newer versions of the standard have added named capture, named backreferences, and mode modifiers. The /u flag adds Unicode categories, blocks, scripts, and even a large set of binary properties. The /u flag also cleans up the syntax by treating invalid escapes and octal escapes as errors. The only significant XRegExp feature that standard JavaScript hasn’t adopted (yet) is free-spacing.

So, for all intents and purposes, while XRegExp was once indispensable for serious regular expression use with JavaScript, it is obsolete with modern browsers. But, if you ever find yourself working in a closed environment using web technology on systems that will never be upgraded from Windows XP, then XRegExp will still come in handy. It’ll also help future-proof your code in case the environment does ever get upgraded. XRegExp still works perfectly well with modern browsers.

How to Use XRegExp

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. $$ 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, avoiding inconsistencies and bugs in old browsers.

| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |

| grep | PowerGREP | RegexBuddy | RegexMagic |

| EditPad Lite | EditPad Pro | Google Docs | Google Sheets | LibreOffice | Notepad++ |

| Boost | C# | Delphi | F# | GNU (Linux) | Groovy | ICU (Unicode) | Java | JavaScript | .NET | PCRE (C/C++) | PCRE2 (C/C++) | Perl | PHP | POSIX | PowerShell | Python | Python.NET and IronPython | R | RE2 | Ruby | std::regex | Tcl | TypeScript | VBScript | Visual Basic 6 | Visual Basic (.NET) | wxWidgets | XML Schema | XQuery & XPath | Xojo | XRegExp |

| Google BigQuery | MySQL | Oracle | PostgreSQL |