Regular Expressions 101

Save & Share

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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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

/
/
gmuiy

Test String

Code Generator

Generated Code

const regex = /^[A-ZÀ|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|Ì|Í|Ị|Ỉ|Ĩ|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|Ỳ|Ý|Ỵ|Ỷ|Ỹ|Đ][a-zà|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|ì|í|ị|ỉ|ĩ|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|ỳ|ý|ỵ|ỷ|ỹ]*(?:[ ][A-ZÀ|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|Ì|Í|Ị|Ỉ|Ĩ|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|Ỳ|Ý|Ỵ|Ỷ|Ỹ|Đ][a-zà|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|ì|í|ị|ỉ|ĩ|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|ỳ|ý|ỵ|ỷ|ỹ]*)*$/gmuiy; // Alternative syntax using RegExp constructor // const regex = new RegExp('^[A-ZÀ|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|Ì|Í|Ị|Ỉ|Ĩ|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|Ỳ|Ý|Ỵ|Ỷ|Ỹ|Đ][a-zà|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|ì|í|ị|ỉ|ĩ|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|ỳ|ý|ỵ|ỷ|ỹ]*(?:[ ][A-ZÀ|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ|È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ|Ì|Í|Ị|Ỉ|Ĩ|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ|Ỳ|Ý|Ỵ|Ỷ|Ỹ|Đ][a-zà|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ|è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ|ì|í|ị|ỉ|ĩ|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ|ỳ|ý|ỵ|ỷ|ỹ]*)*$', 'gmuiy') const str = `aaa Nguyen Van A1 +/** 2 + * @fileoverview This is the messaging service worker. 3 + */ 4 + 5 +importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js'); 6 +importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js'); 7 +importScripts('https://cdn.jsdelivr.net/npm/idb@7/build/umd.js'); 8 +importScripts('project-config.js'); 9 +importScripts('init-firebase.js'); 10 + 11 +// Set the IndexedDB db name, store name and version number. 12 +const dbName = 'push-notifications'; 13 +const storeName = 'pending'; 14 +const dbVersion = 1; 15 + 16 +/** 17 + * onBackgroundMessage receives Firebase messages. 18 + * It saves them, displays a notification, and pokes the client to see if it is 19 + * able to receive messages. 20 + */ 21 +messaging.onBackgroundMessage((msg) => { 22 + console.log('service worker received firebase message', msg); 23 + saveMessage(msg); 24 + const title = msg.data.title; 25 + const options = { 26 + body: msg.data.body, 27 + requireInteraction: true 28 + }; 29 + self.registration.showNotification(title, options); 30 + // Check if the web page is present and can receive messages to display. 31 + channel.postMessage({ 32 + hello: 'check if the client is able to receive messages', 33 + }); 34 +}); 35 + 36 +/** 37 + * When the user clicks on a notification, we switch to our web page if it's 38 + * open, or open it in a new tab if it isn't. 39 + */ 40 +self.addEventListener('notificationclick', (event) => { 41 + event.notification.close(); 42 + event.waitUntil( 43 + clients.matchAll({ type: 'window' }).then((clientList) => { 44 + for (let i = 0; i < clientList.length; i++) { 45 + const client = clientList[i]; 46 + if (client.url == webSiteUrl && 'focus' in client) { 47 + return client.focus(); 48 + } 49 + } 50 + if (clients.openWindow) { 51 + return clients.openWindow(webSiteUrl); 52 + } 53 + }) 54 + ); 55 +}); 56 + 57 +/** 58 + * getDB creates or upgrades the IndexedDB used to store messages and returns 59 + * the database handle. 60 + */ 61 +async function getDB() { 62 + const db = await idb.openDB(dbName, dbVersion, { 63 + upgrade(db, oldVersion) { 64 + console.log('creating or upgrading db'); 65 + switch (oldVersion) { 66 + case 0: 67 + db.createObjectStore(storeName, { 68 + keyPath: 'messageId' 69 + }); 70 + break; 71 + } 72 + } 73 + }); 74 + return db; 75 +} 76 + 77 +/** 78 + * saveMessage writes a Firebase message to the database. 79 + */ 80 +async function saveMessage(msg) { 81 + // Background messages don't have message IDs, so set one using the data 82 + // value. 83 + msg.messageId = msg.data.id; 84 + const db = await getDB(); 85 + db.add(storeName, msg); 86 +} 87 + 88 +/** 89 + * When the service worker is activated, set up the database. 90 + */ 91 +self.addEventListener('activate', (event) => { 92 + getDB(); 93 +}); 94 + 95 +/** 96 + * onmessage is called when we receive a message from the client. 97 + * This is expected to be in response to our message, and signals that the 98 + * client is ready to receive our pending Firebase messages. 99 + * We just throw all the pending messages - we don't wait for acknowledgement 100 + * for each one. 101 + */ 102 +channel.onmessage = async function(cmsg) { 103 + console.log('service worker received channel message', cmsg); 104 + // If the response includes the attribute 'ready', we can send our pending 105 + // messages. 106 + if ('ready' in cmsg.data) { 107 + const db = await getDB(); 108 + console.log(db); 109 + const tx = db.transaction(storeName, 'readwrite'); 110 + const msgs = await tx.store.getAll(); 111 + // Sort the messages by timestamp, so they don't arrive out of order. 112 + msgs.sort((a, b) => { 113 + return (a.timestamp > b.timestamp) ? 1 : ((b.timestamp > a.timestamp) ? -1 : 0); 114 + }); 115 + //const store = tx.objectStore(storeName); 116 + for (let i = 0; i < msgs.length; i++) { 117 + console.log('service worker broadcasting message', msgs[i]); 118 + channel.postMessage({message: msgs[i]}); 119 + console.log('service worker deleting message', msgs[i].messageId); 120 + await tx.store.delete(msgs[i].messageId); 121 + } 122 + await tx.done; 123 + } `; 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