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]
  • Character class intersection
    [\w&&[^\d]]
  • 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 = /(?<=<div.*?)(?<!=\t*?"?\t*?)(class|style)=".*?"/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('(?<=<div.*?)(?<!=\\t*?"?\\t*?)(class|style)=".*?"', 'gm') const str = `<div class='bg-transparent mb-3'> <p>Try this:</p> <pre><code>string html; string cleaned = new Regex(&#34;style=\\&#34;[^\\&#34;]*\\&#34;&#34;).Replace(html, &#34;&#34;); string cleaned = new Regex(&#34;(?&lt;=class=\\&#34;)([^\\&#34;]*)\\\\babc\\\\w*\\\\b([^\\&#34;]*)(?=\\&#34;)&#34;).Replace(cleaned, &#34;\$1\$2&#34;); </code></pre> </div> </div> <div class="col-4"></div> </div> </div><!-- #comments --> <div id="related-embeded" class="related-embeded-area"><div class="row"><div class="col-12"><div class="mt-3 border-bottom border-success"><h4 class="text-info"><i class='fa fa-check-circle text-info mr-3'></i><span>Related Solutions</span></h4></div><div class="mt-3 mb-3 border-bottom"><h5><a href='https://itecnote.com/tecnote/java-remove-html-tags-from-a-string/'>Java &#8211; Remove HTML tags from a String</a></h5></div><div class='bg-transparent mb-3'> <p>Use a HTML parser instead of regex. This is dead simple with <a href="http://jsoup.org" rel="noreferrer">Jsoup</a>.</p> <pre><code>public static String html2text(String html) { return Jsoup.parse(html).text(); } </code></pre> <p>Jsoup also <a href="https://jsoup.org/cookbook/cleaning-html/whitelist-sanitizer" rel="noreferrer">supports</a> removing HTML tags against a customizable whitelist, which is very useful if you want to allow only e.g. <code>&lt;b&gt;</code>, <code>&lt;i&gt;</code> and <code>&lt;u&gt;</code>.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags">RegEx match open tags except XHTML self-contained tags</a></li> <li><a href="https://stackoverflow.com/questions/3152138/what-are-the-pros-and-cons-of-the-leading-java-html-parsers">What are the pros and cons of the leading Java HTML parsers?</a></li> <li><a href="https://stackoverflow.com/questions/2658922/xss-prevention-in-jsp-servlet-web-application">XSS prevention in JSP/Servlet web application</a></li> </ul> </div><div class="mt-3 mb-3 border-bottom"><h5><a href='https://itecnote.com/tecnote/c-how-to-remove-all-non-alphanumeric-characters-from-a-string-except-dash/'>C# &#8211; How to remove all non alphanumeric characters from a string except dash</a></h5></div><div class='bg-transparent mb-3'> <p>Replace <code>[^a-zA-Z0-9 -]</code> with an empty string.</p> <pre><code>Regex rgx = new Regex(&#34;[^a-zA-Z0-9 -]&#34;); str = rgx.Replace(str, &#34;&#34;); </code></pre> </div></div></div></div> </div> <div class="col-12 col-md-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-1962880864575776" data-ad-slot="5946106761" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div class='mt-3 ml-4 border-bottom border-success'><h6><span>Related Question</span></h6></div><ul class='list-group list-group-flush'><li class="list-group-item"><a href='https://itecnote.com/tecnote/html-css-pseudo-classes-with-inline-styles/'>Html &#8211; CSS Pseudo-classes with inline styles</a></li><li class="list-group-item"><a href='https://itecnote.com/tecnote/c-efficient-way-to-remove-all-whitespace-from-string/'>C# &#8211; Efficient way to remove ALL whitespace from String</a></li><li class="list-group-item"><a href='https://itecnote.com/tecnote/javascript-how-to-remove-all-line-breaks-from-a-string/'>Javascript &#8211; How to remove all line breaks from a string</a></li><li class="list-group-item"><a href='https://itecnote.com/tecnote/c-how-to-remove-all-html-tags-from-a-string-without-knowing-which-tags-are-in-it/'>C# &#8211; How to remove all HTML tags from a string without knowing which tags are in it</a></li><li class="list-group-item"><a href='https://itecnote.com/tecnote/html-how-to-override-bootstrap-css-styles/'>Html &#8211; How to override Bootstrap CSS styles</a></li><li class="list-group-item"><a href='https://itecnote.com/tecnote/c-why-not-inherit-from-list/'>C# &#8211; Why not inherit from List<T></a></li><li class="list-group-item"><a href='https://itecnote.com/tecnote/html-stylesheet-not-loaded-because-of-mime-type/'>Html &#8211; Stylesheet not loaded because of MIME-type</a></li></ul> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-1962880864575776" data-ad-slot="1527980765" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div>`; // 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