Regular Expressions 101

Save & Share

  • Regex Version: ver. 1
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
No Match

/
/

Test String

Code Generator

Generated Code

const regex = /_gpfErrorDeclare\("([a-zA-Z\\]+)", {\n((?:.*\n)*)\s*}\)/; // Alternative syntax using RegExp constructor // const regex = new RegExp('_gpfErrorDeclare\\("([a-zA-Z\\\\]+)", {\\n((?:.*\\n)*)\\s*}\\)', '') const str = `/** * @file Error base class * @since 0.1.5 */ /*#ifndef(UMD)*/ "use strict"; /*global _gpfExtend*/ // gpf.extend /*global _gpfIgnore*/ // Helper to remove unused parameter warning /*global _gpfObjectForEach*/ // Similar to [].forEach but for objects /*global _gpfStringCapitalize*/ // Capitalize the string /*global _gpfStringReplaceEx*/ // String replacement using dictionary map /*exported _gpfErrorDeclare*/ // Declare new gpf.Error names /*#endif*/ /** * GPF Error class * * @constructor * @alias gpf.Error * @since 0.1.5 */ var _GpfError = gpf.Error = function () {}; _GpfError.prototype = new Error(); _gpfExtend(_GpfError.prototype, /** @lends gpf.Error.prototype */ { constructor: _GpfError, /** * Error code * * @readonly * @since 0.1.5 */ code: 0, /** * Error name * * @readonly * @since 0.1.5 */ name: "Error", /** * Error message * * @readonly * @since 0.1.5 */ message: "", /** * Build message by substituting context variables * * @param {Object} context Dictionary of named keys * @since 0.1.5 */ _buildMessage: function (context) { var replacements; if (context) { replacements = {}; _gpfObjectForEach(context, function (value, key) { replacements["{" + key + "}"] = value.toString(); }); this.message = _gpfStringReplaceEx(this.message, replacements); } } }); function _gpfErrorFactory (code, name, message) { function NewErrorClass (context) { this._buildMessage(context); } NewErrorClass.prototype = new _GpfError(); _gpfExtend(NewErrorClass.prototype, { code: code, name: name, message: message }); // constructor can't be enumerated with wscript NewErrorClass.prototype.constructor = NewErrorClass; _GpfError[_gpfStringCapitalize(name)] = NewErrorClass; return function (context) { throw new NewErrorClass(context); }; } /** * Generates an error class * * @param {Number} code Error code * @param {String} name Error name * @param {String} message Error message * @return {Function} New error class * @gpf:closure * @since 0.1.5 */ function _gpfGenenerateErrorFunction (code, name, message) { var result = _gpfErrorFactory(code, name, message); result.CODE = code; result.NAME = name; result.MESSAGE = message; return result; } // Last allocated error code var _gpfLastErrorCode = 0; /** * Declare error messages. * Each source declares its own errors. * * @param {String} source Source name * @param {Object} dictionary Dictionary of error name to message * @since 0.1.5 */ function _gpfErrorDeclare (source, dictionary) { _gpfIgnore(source); _gpfObjectForEach(dictionary, function (message, name) { var code = ++_gpfLastErrorCode; gpf.Error["CODE_" + name.toUpperCase()] = code; gpf.Error[name] = _gpfGenenerateErrorFunction(code, name, message); }); } _gpfErrorDeclare("error", { /** * ### Summary * * Method or function is not implemented * * ### Description * * This error is used to flag methods or functions that are not yet implemented. * @since 0.1.5 */ notImplemented: "Not implemented", /** * ### Summary * * Method is abstract * * ### Description * * This error is used to implement abstract methods. Mostly used for interfaces. * @since 0.1.5 */ abstractMethod: "Abstract method", /** * ### Summary * * An assertion failed * * ### Description * * This error is triggered when an assertion fails * * @see {@link gpf.assert} * @see {@link gpf.asserts} * @since 0.1.5 */ assertionFailed: "Assertion failed: {message}", /** * ### Summary * * Method or function was called with an invalid parameter * * ### Description * * This error is used when a parameter is invalid * @since 0.1.5 */ invalidParameter: "Invalid parameter" }); `; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; if ((m = regex.exec(str)) !== null) { // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions