Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

Test String

Code Generator

Generated Code

const regex = /\G(?|(?:(?:\s+)(*MARK:T_WHITESPACE))|(?:(?:\/\/[^\n]*\n)(*MARK:T_COMMENT))|(?:(?:(?<=\b)true\b)(*MARK:T_BOOL_TRUE))|(?:(?:(?<=\b)false\b)(*MARK:T_BOOL_FALSE))|(?:(?:(?<=\b)null\b)(*MARK:T_NULL))|(?:(?:(?<=\b)Project\b)(*MARK:T_PROJECT))|(?:(?:(?<=\b)Table\b)(*MARK:T_TABLE))|(?:(?:(?<=\b)as\b)(*MARK:T_TABLE_ALIAS))|(?:(?:(?<=\b)(Indexes|indexes)\b)(*MARK:T_TABLE_INDEXES))|(?:(?:(Ref|ref))(*MARK:T_TABLE_REF))|(?:(?:(?<=\b)TableGroup\b)(*MARK:T_TABLE_GROUP))|(?:(?:(?<=\b)(Enum|enum)\b)(*MARK:T_ENUM))|(?:(?:(?<=\b)(primary\ske|pk)\b)(*MARK:T_TABLE_SETTING_PK))|(?:(?:(?<=\b)unique\b)(*MARK:T_TABLE_SETTING_UNIQUE))|(?:(?:(?<=\b)increment\b)(*MARK:T_TABLE_SETTING_INCREMENT))|(?:(?:(?<=\b)default\b)(*MARK:T_TABLE_SETTING_DEFAULT))|(?:(?:(?<=\b)null\b)(*MARK:T_TABLE_SETTING_NULL))|(?:(?:(?<=\b)not\snull\b)(*MARK:T_TABLE_SETTING_NOT_NULL))|(?:(?:(?<=\b)cascade\b)(*MARK:T_REF_ACTION_CASCADE))|(?:(?:(?<=\b)restrict\b)(*MARK:T_REF_ACTION_RESTRICT))|(?:(?:(?<=\b)set\snull\b)(*MARK:T_REF_ACTION_SET_NULL))|(?:(?:(?<=\b)set\default\b)(*MARK:T_REF_ACTION_SET_DEFAULT))|(?:(?:(?<=\b)no\saction\b)(*MARK:T_REF_ACTION_NO_ACTION))|(?:(?:(?<=\b)delete\b)(*MARK:T_REF_ACTION_DELETE))|(?:(?:(?<=\b)update\b)(*MARK:T_REF_ACTION_UPDATE))|(?:(?:note:)(*MARK:T_SETTING_NOTE))|(?:(?:(?<=\b)Note\b)(*MARK:T_NOTE))|(?:(?:[0-9]+\.[0-9]+)(*MARK:T_FLOAT))|(?:(?:[0-9]+)(*MARK:T_INT))|(?:(?:('{3}|["']{1})([^'"][\s\S]*?)\1)(*MARK:T_QUOTED_STRING))|(?:(?:(`{1})([\s\S]+?)\1)(*MARK:T_EXPRESSION))|(?:(?:[a-zA-Z0-9_]+)(*MARK:T_WORD))|(?:(?:\\n)(*MARK:T_EOL))|(?:(?:\()(*MARK:T_LPAREN))|(?:(?:\))(*MARK:T_RPAREN))|(?:(?:{)(*MARK:T_LBRACE))|(?:(?:})(*MARK:T_RBRACE))|(?:(?:\[)(*MARK:T_LBRACK))|(?:(?:\])(*MARK:T_RBRACK))|(?:(?:\>)(*MARK:T_GT))|(?:(?:\<)(*MARK:T_LT))|(?:(?:,)(*MARK:T_COMMA))|(?:(?::)(*MARK:T_COLON))|(?:(?:\-)(*MARK:T_MINUS))|(?:(?:\.)(*MARK:T_DOT))|(?:(?:.+?)(*MARK:T_UNKNOWN)))/sumg; // Alternative syntax using RegExp constructor // const regex = new RegExp('\\G(?|(?:(?:\\s+)(*MARK:T_WHITESPACE))|(?:(?:\\\/\\\/[^\\n]*\\n)(*MARK:T_COMMENT))|(?:(?:(?<=\\b)true\\b)(*MARK:T_BOOL_TRUE))|(?:(?:(?<=\\b)false\\b)(*MARK:T_BOOL_FALSE))|(?:(?:(?<=\\b)null\\b)(*MARK:T_NULL))|(?:(?:(?<=\\b)Project\\b)(*MARK:T_PROJECT))|(?:(?:(?<=\\b)Table\\b)(*MARK:T_TABLE))|(?:(?:(?<=\\b)as\\b)(*MARK:T_TABLE_ALIAS))|(?:(?:(?<=\\b)(Indexes|indexes)\\b)(*MARK:T_TABLE_INDEXES))|(?:(?:(Ref|ref))(*MARK:T_TABLE_REF))|(?:(?:(?<=\\b)TableGroup\\b)(*MARK:T_TABLE_GROUP))|(?:(?:(?<=\\b)(Enum|enum)\\b)(*MARK:T_ENUM))|(?:(?:(?<=\\b)(primary\\ske|pk)\\b)(*MARK:T_TABLE_SETTING_PK))|(?:(?:(?<=\\b)unique\\b)(*MARK:T_TABLE_SETTING_UNIQUE))|(?:(?:(?<=\\b)increment\\b)(*MARK:T_TABLE_SETTING_INCREMENT))|(?:(?:(?<=\\b)default\\b)(*MARK:T_TABLE_SETTING_DEFAULT))|(?:(?:(?<=\\b)null\\b)(*MARK:T_TABLE_SETTING_NULL))|(?:(?:(?<=\\b)not\\snull\\b)(*MARK:T_TABLE_SETTING_NOT_NULL))|(?:(?:(?<=\\b)cascade\\b)(*MARK:T_REF_ACTION_CASCADE))|(?:(?:(?<=\\b)restrict\\b)(*MARK:T_REF_ACTION_RESTRICT))|(?:(?:(?<=\\b)set\\snull\\b)(*MARK:T_REF_ACTION_SET_NULL))|(?:(?:(?<=\\b)set\\default\\b)(*MARK:T_REF_ACTION_SET_DEFAULT))|(?:(?:(?<=\\b)no\\saction\\b)(*MARK:T_REF_ACTION_NO_ACTION))|(?:(?:(?<=\\b)delete\\b)(*MARK:T_REF_ACTION_DELETE))|(?:(?:(?<=\\b)update\\b)(*MARK:T_REF_ACTION_UPDATE))|(?:(?:note:)(*MARK:T_SETTING_NOTE))|(?:(?:(?<=\\b)Note\\b)(*MARK:T_NOTE))|(?:(?:[0-9]+\\.[0-9]+)(*MARK:T_FLOAT))|(?:(?:[0-9]+)(*MARK:T_INT))|(?:(?:(\'{3}|["\']{1})([^\'"][\\s\\S]*?)\\1)(*MARK:T_QUOTED_STRING))|(?:(?:(`{1})([\\s\\S]+?)\\1)(*MARK:T_EXPRESSION))|(?:(?:[a-zA-Z0-9_]+)(*MARK:T_WORD))|(?:(?:\\\\n)(*MARK:T_EOL))|(?:(?:\\()(*MARK:T_LPAREN))|(?:(?:\\))(*MARK:T_RPAREN))|(?:(?:{)(*MARK:T_LBRACE))|(?:(?:})(*MARK:T_RBRACE))|(?:(?:\\[)(*MARK:T_LBRACK))|(?:(?:\\])(*MARK:T_RBRACK))|(?:(?:\\>)(*MARK:T_GT))|(?:(?:\\<)(*MARK:T_LT))|(?:(?:,)(*MARK:T_COMMA))|(?:(?::)(*MARK:T_COLON))|(?:(?:\\-)(*MARK:T_MINUS))|(?:(?:\\.)(*MARK:T_DOT))|(?:(?:.+?)(*MARK:T_UNKNOWN)))', 'sumg') const str = `Project test { database_type: 'PostgreSQL' Note: 'Description of the project' } //// -- LEVEL 1 //// -- Tables and References // Creating tables Table users as U { id int [pk, unique, increment] // auto-increment full_name varchar [not null, unique, default: 1] created_at timestamp country_code int type int note int Note: 'khong hieu duoc' } Table merchants { id int } Table countries { code int [pk] name varchar continent_name varchar } // Creating references // You can also define relationship separately // > many-to-one; < one-to-many; - one-to-one Ref{ U.country_code > countries.code merchants.country_code > countries.code } //----------------------------------------------// //// -- LEVEL 2 //// -- Adding column settings Table order_items { order_id int [ref: > orders.id] product_id int quantity int [default: 1] // default value } Ref: order_items.product_id > products.id Table orders { id int [pk] // primary key user_id int [not null, unique] status varchar created_at varchar [note: '''When order created'''] // add column note } Table int { id int } //----------------------------------------------// //// -- Level 3 //// -- Enum, Indexes // Enum for 'products' table below Enum products_status { out_of_stock in_stock running_low [note: 'less than 20'] // add column note } // Indexes: You can define a single or multi-column index Table products { id int [pk] name varchar merchant_id int [not null] price int status products_status created_at datetime [default: \`now()\`] Indexes { (merchant_id, status) [name:'product_status', type: hash] id [unique] } } Ref: products.merchant_id > merchants.id // many-to-one TableGroup hello_world { just_test just_a_test } `; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // 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