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
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

/
/
gm

Test String

Code Generator

Generated Code

const regex = /<(.*)(?:\s[^>]*?)?>(?:{{)([^<]*?)<\/\1>([^<]*?<[\s\S]*?>[^>]*?)<(.*)(?:\s[^>]*?)?>([^<]*)(?:}})<\/\4>/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('<(.*)(?:\\s[^>]*?)?>(?:{{)([^<]*?)<\\\/\\1>([^<]*?<[\\s\\S]*?>[^>]*?)<(.*)(?:\\s[^>]*?)?>([^<]*)(?:}})<\\\/\\4>', 'gm') const str = `<p style="margin-top:0pt; margin-bottom:0pt; font-size:15pt"><span style="font-family:宋体">乙方经办人</span><span style="font-family:宋体; color:#e36c0a"> </span><span style="font-family:宋体; font-weight:bold; text-decoration:underline; color:#e36c0a; -aw-import:spaces">&#xa0; </span><span style="font-family:Calibri; font-weight:bold; text-decoration:underline; color:#e36c0a">{{</span><span style="font-family:宋体; font-weight:bold; text-decoration:underline; color:#e36c0a">甲方手机号</span><span style="font-family:Calibri; font-weight:bold; text-decoration:underline; color:#e36c0a">}} </span></p> <div align="center"> <hr size="2" align="center" noshade="" style="width:100%; color:#444444; background-color:#444444" /> </div> <p style="margin-top:0pt; margin-bottom:0pt; line-height:22.5pt; background-color:#ffffff"><span style="font-family:宋体; font-size:10.5pt; color:#444444">我问:</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">“</span><span style="font-family:宋体; font-size:10.5pt; color:#444444">魔镜啊魔镜,哪块才是这个世界上最易碎的玻璃?</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">”</span><br /><span style="font-family:宋体; font-size:10.5pt; color:#444444">魔镜说:</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">“</span><span style="font-family:宋体; font-size:10.5pt; color:#444444">晕!你最美!你最美行了吧。。。</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">”</span></p> <div align="center"> <hr size="2" align="center" noshade="" style="width:100%; color:#444444; background-color:#444444" /> </div> <p style="margin-top:0pt; margin-bottom:0pt; line-height:22.5pt; background-color:#ffffff"><span style="font-family:宋体; font-size:10.5pt; color:#444444">买了块几十万的表,结果每天都跑得太快,还不如几十块钱的电子表。</span><br /><span style="font-family:宋体; font-size:10.5pt; color:#444444">我问厂家怎么回事,厂家说:</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">“</span><span style="font-family:宋体; font-size:10.5pt; color:#444444">法拉利是不是比夏利跑得快?对吧?那这个名表当然要比普通表快了!</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">”</span></p> <div align="center"> <hr size="2" align="center" noshade="" style="width:100%; color:#444444; background-color:#444444" /> </div> <p style="margin-top:0pt; margin-bottom:0pt; line-height:22.5pt; background-color:#ffffff"><span style="font-family:宋体; font-size:10.5pt; color:#444444">儿子说:</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">“</span><span style="font-family:宋体; font-size:10.5pt; color:#444444">妈妈,包子一定是馒头的妈妈吧。</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">”</span><br /><span style="font-family:宋体; font-size:10.5pt; color:#444444">妈妈:</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">“</span><span style="font-family:宋体; font-size:10.5pt; color:#444444">为什么?</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">”</span><br /><span style="font-family:宋体; font-size:10.5pt; color:#444444">儿子:</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">“</span><span style="font-family:宋体; font-size:10.5pt; color:#444444">你看,它长皱纹了。</span><span style="font-family:Verdana; font-size:10.5pt; color:#444444">”</span></p> <div align="center"> <hr size="2" align="center" noshade="" style="width:100%; color:#444444; background-color:#444444" /> </div> <p style="margin-top:7.5pt; margin-bottom:12pt; line-height:22.5pt; background-color:#ffffff"><span style="font-family:宋体; font-size:10.5pt; color:#444444">爱吃的女孩运气都不会太差,因为食来运转嘛。</span></p> <div align="center"> <hr size="2" align="center" noshade="" style="width:100%; color:#444444; background-color:#444444" /> </div> <p style="margin-top:7.5pt; margin-bottom:12pt; line-height:22.5pt; background-color:#ffffff"><span style="font-family:Helvetica; font-size:10.5pt; font-weight:bold; color:#383838; background-color:#f5f6f9">6</span><span style="font-family:宋体; font-size:10.5pt; font-weight:bold; color:#383838; background-color:#f5f6f9">页</span><span style="font-family:宋体; font-size:10.5pt; color:#444444">今天客户说我们公司的设备卡,死活找不到原因,工程师赶到现场,给客户换了个鼠标垫,故障排除。。。</span></p>`; // 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