-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathregular-expressions.html
More file actions
158 lines (129 loc) · 54.2 KB
/
regular-expressions.html
File metadata and controls
158 lines (129 loc) · 54.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>JavsScript - Regular expressions</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="Creating a RegExp Object, RegExp Flags, Check if string contains pattern using .test(), Matching With .exec(), Using RegExp With Strings, RegExp Groups, Replacing string match with a callback function, Using Regex.exec() with parentheses regex to extract matches of a string">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="JavsScript - Regular expressions">
<meta property="og:description" content="Creating a RegExp Object, RegExp Flags, Check if string contains pattern using .test(), Matching With .exec(), Using RegExp With Strings, RegExp Groups, Replacing string match with a callback function, Using Regex.exec() with parentheses regex to extract matches of a string">
<meta property="og:type" content="article">
<meta property="og:url" content="/javascript/regular-expressions.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="JavsScript - Regular expressions">
<meta name="twitter:description" content="Creating a RegExp Object, RegExp Flags, Check if string contains pattern using .test(), Matching With .exec(), Using RegExp With Strings, RegExp Groups, Replacing string match with a callback function, Using Regex.exec() with parentheses regex to extract matches of a string">
<meta name="twitter:url" content="/javascript/regular-expressions.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/1775.86753daa.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>JavaScript</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/javascript/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/javascript/getting-started-with-javascript.html" class="sidebar-link">Getting started with JavaScript</a></li><li><a href="/javascript/javascript-variables.html" class="sidebar-link">JavaScript Variables</a></li><li><a href="/javascript/built-in-constants.html" class="sidebar-link">Built-in Constants</a></li><li><a href="/javascript/comments.html" class="sidebar-link">Comments</a></li><li><a href="/javascript/console.html" class="sidebar-link">Console</a></li><li><a href="/javascript/datatypes-in-javascript.html" class="sidebar-link">Datatypes in Javascript</a></li><li><a href="/javascript/strings.html" class="sidebar-link">Strings</a></li><li><a href="/javascript/date.html" class="sidebar-link">Date</a></li><li><a href="/javascript/date-comparison.html" class="sidebar-link">Date Comparison</a></li><li><a href="/javascript/comparison-operations.html" class="sidebar-link">Comparison Operations</a></li><li><a href="/javascript/conditions.html" class="sidebar-link">Conditions</a></li><li><a href="/javascript/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/javascript/objects.html" class="sidebar-link">Objects</a></li><li><a href="/javascript/arithmetic-math.html" class="sidebar-link">Arithmetic (Math)</a></li><li><a href="/javascript/bitwise-operators.html" class="sidebar-link">Bitwise operators</a></li><li><a href="/javascript/constructor-functions.html" class="sidebar-link">Constructor functions</a></li><li><a href="/javascript/declarations-and-assignments.html" class="sidebar-link">Declarations and Assignments</a></li><li><a href="/javascript/loops.html" class="sidebar-link">Loops</a></li><li><a href="/javascript/functions.html" class="sidebar-link">Functions</a></li><li><a href="/javascript/functional-javascript.html" class="sidebar-link">Functional JavaScript</a></li><li><a href="/javascript/prototypes-objects.html" class="sidebar-link">Prototypes, objects</a></li><li><a href="/javascript/classes.html" class="sidebar-link">Classes</a></li><li><a href="/javascript/namespacing.html" class="sidebar-link">Namespacing</a></li><li><a href="/javascript/context-this.html" class="sidebar-link">Context (this)</a></li><li><a href="/javascript/setters-and-getters.html" class="sidebar-link">Setters and Getters</a></li><li><a href="/javascript/events.html" class="sidebar-link">Events</a></li><li><a href="/javascript/inheritance.html" class="sidebar-link">Inheritance</a></li><li><a href="/javascript/method-chaining.html" class="sidebar-link">Method Chaining</a></li><li><a href="/javascript/callbacks.html" class="sidebar-link">Callbacks</a></li><li><a href="/javascript/intervals-and-timeouts.html" class="sidebar-link">Intervals and Timeouts</a></li><li><a href="/javascript/regular-expressions.html" aria-current="page" class="active sidebar-link">Regular expressions</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/javascript/regular-expressions.html#creating-a-regexp-object" class="sidebar-link">Creating a RegExp Object</a></li><li class="sidebar-sub-header"><a href="/javascript/regular-expressions.html#regexp-flags" class="sidebar-link">RegExp Flags</a></li><li class="sidebar-sub-header"><a href="/javascript/regular-expressions.html#check-if-string-contains-pattern-using-test" class="sidebar-link">Check if string contains pattern using .test()</a></li><li class="sidebar-sub-header"><a href="/javascript/regular-expressions.html#matching-with-exec" class="sidebar-link">Matching With .exec()</a></li><li class="sidebar-sub-header"><a href="/javascript/regular-expressions.html#using-regexp-with-strings" class="sidebar-link">Using RegExp With Strings</a></li><li class="sidebar-sub-header"><a href="/javascript/regular-expressions.html#regexp-groups" class="sidebar-link">RegExp Groups</a></li><li class="sidebar-sub-header"><a href="/javascript/regular-expressions.html#replacing-string-match-with-a-callback-function" class="sidebar-link">Replacing string match with a callback function</a></li><li class="sidebar-sub-header"><a href="/javascript/regular-expressions.html#using-regex-exec-with-parentheses-regex-to-extract-matches-of-a-string" class="sidebar-link">Using Regex.exec() with parentheses regex to extract matches of a string</a></li></ul></li><li><a href="/javascript/cookies.html" class="sidebar-link">Cookies</a></li><li><a href="/javascript/web-storage.html" class="sidebar-link">Web Storage</a></li><li><a href="/javascript/data-attributes.html" class="sidebar-link">Data attributes</a></li><li><a href="/javascript/json.html" class="sidebar-link">JSON</a></li><li><a href="/javascript/ajax.html" class="sidebar-link">AJAX</a></li><li><a href="/javascript/enumerations.html" class="sidebar-link">Enumerations</a></li><li><a href="/javascript/map.html" class="sidebar-link">Map</a></li><li><a href="/javascript/timestamps.html" class="sidebar-link">Timestamps</a></li><li><a href="/javascript/unary-operators.html" class="sidebar-link">Unary Operators</a></li><li><a href="/javascript/generators.html" class="sidebar-link">Generators</a></li><li><a href="/javascript/promises.html" class="sidebar-link">Promises</a></li><li><a href="/javascript/set.html" class="sidebar-link">Set</a></li><li><a href="/javascript/modals-prompts.html" class="sidebar-link">Modals - Prompts</a></li><li><a href="/javascript/execcommand-and-contenteditable.html" class="sidebar-link">execCommand and contenteditable</a></li><li><a href="/javascript/history.html" class="sidebar-link">History</a></li><li><a href="/javascript/navigator-object.html" class="sidebar-link">Navigator Object</a></li><li><a href="/javascript/bom-browser-object-model.html" class="sidebar-link">BOM (Browser Object Model)</a></li><li><a href="/javascript/the-event-loop.html" class="sidebar-link">The Event Loop</a></li><li><a href="/javascript/strict-mode.html" class="sidebar-link">Strict mode</a></li><li><a href="/javascript/custom-elements.html" class="sidebar-link">Custom Elements</a></li><li><a href="/javascript/data-manipulation.html" class="sidebar-link">Data Manipulation</a></li><li><a href="/javascript/binary-data.html" class="sidebar-link">Binary Data</a></li><li><a href="/javascript/template-literals.html" class="sidebar-link">Template Literals</a></li><li><a href="/javascript/fetch.html" class="sidebar-link">Fetch</a></li><li><a href="/javascript/scope.html" class="sidebar-link">Scope</a></li><li><a href="/javascript/modules.html" class="sidebar-link">Modules</a></li><li><a href="/javascript/screen.html" class="sidebar-link">Screen</a></li><li><a href="/javascript/variable-coercion-conversion.html" class="sidebar-link">Variable coercion/conversion</a></li><li><a href="/javascript/destructuring-assignment.html" class="sidebar-link">Destructuring assignment</a></li><li><a href="/javascript/websockets.html" class="sidebar-link">WebSockets</a></li><li><a href="/javascript/arrow-functions.html" class="sidebar-link">Arrow Functions</a></li><li><a href="/javascript/workers.html" class="sidebar-link">Workers</a></li><li><a href="/javascript/requestanimationframe.html" class="sidebar-link">requestAnimationFrame</a></li><li><a href="/javascript/creational-design-patterns.html" class="sidebar-link">Creational Design Patterns</a></li><li><a href="/javascript/detecting-browser.html" class="sidebar-link">Detecting browser</a></li><li><a href="/javascript/symbols.html" class="sidebar-link">Symbols</a></li><li><a href="/javascript/transpiling.html" class="sidebar-link">Transpiling</a></li><li><a href="/javascript/automatic-semicolon-insertion-asi.html" class="sidebar-link">Automatic Semicolon Insertion - ASI</a></li><li><a href="/javascript/localization.html" class="sidebar-link">Localization</a></li><li><a href="/javascript/geolocation.html" class="sidebar-link">Geolocation</a></li><li><a href="/javascript/indexeddb.html" class="sidebar-link">IndexedDB</a></li><li><a href="/javascript/modularization-techniques.html" class="sidebar-link">Modularization Techniques</a></li><li><a href="/javascript/proxy.html" class="sidebar-link">Proxy</a></li><li><a href="/javascript/postmessage-and-messageevent.html" class="sidebar-link">.postMessage() and MessageEvent</a></li><li><a href="/javascript/weakmap.html" class="sidebar-link">WeakMap</a></li><li><a href="/javascript/weakset.html" class="sidebar-link">WeakSet</a></li><li><a href="/javascript/escape-sequences.html" class="sidebar-link">Escape Sequences</a></li><li><a href="/javascript/behavioral-design-patterns.html" class="sidebar-link">Behavioral Design Patterns</a></li><li><a href="/javascript/server-sent-events.html" class="sidebar-link">Server-sent events</a></li><li><a href="/javascript/async-functions-async-await.html" class="sidebar-link">Async functions (async/await)</a></li><li><a href="/javascript/async-iterators.html" class="sidebar-link">Async Iterators</a></li><li><a href="/javascript/how-to-make-iterator-usable-inside-async-callback-function.html" class="sidebar-link">How to make iterator usable inside async callback function</a></li><li><a href="/javascript/tail-call-optimization.html" class="sidebar-link">Tail Call Optimization</a></li><li><a href="/javascript/bitwise-operators-real-world-examples-snippets.html" class="sidebar-link">Bitwise Operators - Real World Examples (snippets)</a></li><li><a href="/javascript/tilde.html" class="sidebar-link">Tilde ~</a></li><li><a href="/javascript/using-javascript-to-get-set-css-custom-variables.html" class="sidebar-link">Using javascript to get/set CSS custom variables</a></li><li><a href="/javascript/selection-api.html" class="sidebar-link">Selection API</a></li><li><a href="/javascript/file-api-blobs-and-filereaders.html" class="sidebar-link">File API, Blobs and FileReaders</a></li><li><a href="/javascript/notifications-api.html" class="sidebar-link">Notifications API</a></li><li><a href="/javascript/vibration-api.html" class="sidebar-link">Vibration API</a></li><li><a href="/javascript/battery-status-api.html" class="sidebar-link">Battery Status API</a></li><li><a href="/javascript/fluent-api.html" class="sidebar-link">Fluent API</a></li><li><a href="/javascript/web-cryptography-api.html" class="sidebar-link">Web Cryptography API</a></li><li><a href="/javascript/security-issues.html" class="sidebar-link">Security issues</a></li><li><a href="/javascript/same-origin-policy-cross-origin-communication.html" class="sidebar-link">Same Origin Policy & Cross-Origin Communication</a></li><li><a href="/javascript/error-handling.html" class="sidebar-link">Error Handling</a></li><li><a href="/javascript/global-error-handling-in-browsers.html" class="sidebar-link">Global error handling in browsers</a></li><li><a href="/javascript/debugging.html" class="sidebar-link">Debugging</a></li><li><a href="/javascript/unit-testing-javascript.html" class="sidebar-link">Unit Testing Javascript</a></li><li><a href="/javascript/evaluating-javascript.html" class="sidebar-link">Evaluating JavaScript</a></li><li><a href="/javascript/linters-ensuring-code-quality.html" class="sidebar-link">Linters - Ensuring code quality</a></li><li><a href="/javascript/anti-patterns.html" class="sidebar-link">Anti-patterns</a></li><li><a href="/javascript/performance-tips.html" class="sidebar-link">Performance Tips</a></li><li><a href="/javascript/memory-efficiency.html" class="sidebar-link">Memory efficiency</a></li><li><a href="/javascript/reserved-keywords.html" class="sidebar-link">Reserved Keywords</a></li><li><a href="/javascript/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="regular-expressions"><a href="#regular-expressions" class="header-anchor">#</a> Regular expressions</h1> <h2 id="creating-a-regexp-object"><a href="#creating-a-regexp-object" class="header-anchor">#</a> Creating a RegExp Object</h2> <h3 id="standard-creation"><a href="#standard-creation" class="header-anchor">#</a> Standard Creation</h3> <p>It is recommended to use this form only when creating regex from dynamic variables.</p> <p>Use when the expression may change or the expression is user generated.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">".*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>With flags:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">".*"</span><span class="token punctuation">,</span> <span class="token string">"gmi"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>With a backslash: (this must be escaped because the regex is specified with a string)</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">"\\w*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="static-initialization"><a href="#static-initialization" class="header-anchor">#</a> Static initialization</h3> <p>Use when you know the regular expression will not change, and you know what the expression is before runtime.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">.*</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
</code></pre></div><p>With flags:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">.*</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gmi</span></span><span class="token punctuation">;</span>
</code></pre></div><p>With a backslash: (this should not be escaped because the regex is specified in a literal)</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\w*</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
</code></pre></div><h2 id="regexp-flags"><a href="#regexp-flags" class="header-anchor">#</a> RegExp Flags</h2> <p>There are several flags you can specify to alter the RegEx behaviour. Flags may be appended to the end of a regex literal, such as specifying <code>gi</code> in <code>/test/gi</code>, or they may be specified as the second argument to the <code>RegExp</code> constructor, as in <code>new RegExp('test', 'gi')</code>.</p> <p><code>g</code> - Global. Finds all matches instead of stopping after the first.</p> <p><code>i</code> - Ignore case. <code>/[a-z]/i</code> is equivalent to <code>/[a-zA-Z]/</code>.</p> <p><code>m</code> - Multiline. <code>^</code> and <code>$</code> match the beginning and end of each line respectively treating <code>\n</code> and <code>\r</code> as delimiters instead of simply the beginning and end of the entire string.</p> <p><code>u</code> - Unicode. If this flag is not supported you must match specific Unicode characters with <code>\uXXXX</code> where <code>XXXX</code> is the character's value in hexadecimal.</p> <p><code>y</code> - Finds all consecutive/adjacent matches.</p> <h2 id="check-if-string-contains-pattern-using-test"><a href="#check-if-string-contains-pattern-using-test" class="header-anchor">#</a> Check if string contains pattern using .test()</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[a-z]+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>re<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">"foo"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Match exists."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>The <code>test</code> method performs a search to see if a regular expression matches a string. The regular expression <code>[a-z]+</code> will search for one or more lowercase letters. Since the pattern matches the string, “match exists” will be logged to the console.</p> <h2 id="matching-with-exec"><a href="#matching-with-exec" class="header-anchor">#</a> Matching With .exec()</h2> <h3 id="match-using-exec"><a href="#match-using-exec" class="header-anchor">#</a> Match Using <code>.exec()</code></h3> <p><code>RegExp.prototype.exec(string)</code> returns an array of captures, or <code>null</code> if there was no match.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">([0-9]+)[a-z]+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
<span class="token keyword">var</span> match <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">"foo123bar"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><code>match.index</code> is 3, the (zero-based) location of the match.</p> <p><code>match[0]</code> is the full match string.</p> <p><code>match[1]</code> is the text corresponding to the first captured group. <code>match[n]</code> would be the value of the <strong>n</strong>th captured group.</p> <h3 id="loop-through-matches-using-exec"><a href="#loop-through-matches-using-exec" class="header-anchor">#</a> Loop Through Matches Using <code>.exec()</code></h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result<span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>result <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'barbatbaz'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"found '"</span> <span class="token operator">+</span> result<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">"', next exec starts at index '"</span> <span class="token operator">+</span> re<span class="token punctuation">.</span>lastIndex <span class="token operator">+</span> <span class="token string">"'"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Expected output</p> <blockquote></blockquote> <p>found 'a', next exec starts at index '2'<br>
found 'a', next exec starts at index '5'<br>
found 'a', next exec starts at index '8'</p> <h2 id="using-regexp-with-strings"><a href="#using-regexp-with-strings" class="header-anchor">#</a> Using RegExp With Strings</h2> <p>The String object has the following methods that accept regular expressions as arguments.</p> <ul><li><code>"string".match(...</code></li> <li><code>"string".replace(...</code></li> <li><code>"string".split(...</code></li> <li><code>"string".search(...</code></li></ul> <h3 id="match-with-regexp"><a href="#match-with-regexp" class="header-anchor">#</a> Match with RegExp</h3> <div class="language-js extra-class"><pre class="language-js"><code>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"string"</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[i-n]+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"string"</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(r)[i-n]+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Expected output</p> <blockquote></blockquote> <p>Array ["in"]<br>
Array ["rin", "r"]</p> <h3 id="replace-with-regexp"><a href="#replace-with-regexp" class="header-anchor">#</a> Replace with RegExp</h3> <div class="language-js extra-class"><pre class="language-js"><code>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"string"</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[i-n]+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token string">"foo"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Expected output</p> <blockquote></blockquote> <p>strfoog</p> <h3 id="split-with-regexp"><a href="#split-with-regexp" class="header-anchor">#</a> Split with RegExp</h3> <div class="language-js extra-class"><pre class="language-js"><code>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"stringstring"</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[i-n]+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Expected output</p> <blockquote></blockquote> <p>Array ["str", "gstr", "g"]</p> <h3 id="search-with-regexp"><a href="#search-with-regexp" class="header-anchor">#</a> Search with RegExp</h3> <p><code>.search()</code> returns the index at which a match is found or -1.</p> <div class="language-js extra-class"><pre class="language-js"><code>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"string"</span><span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[i-n]+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"string"</span><span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[o-q]+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Expected output</p> <blockquote></blockquote> <p>3<br>
-1</p> <h2 id="regexp-groups"><a href="#regexp-groups" class="header-anchor">#</a> RegExp Groups</h2> <p>JavaScript supports several types of group in it's Regular Expressions, <strong>capture groups</strong>, <strong>non-capture groups</strong> and <strong>look-aheads</strong>. Currently, there is no <strong>look-behind</strong> support.</p> <h3 id="capture"><a href="#capture" class="header-anchor">#</a> Capture</h3> <p>Sometimes the desired match relies on it's context. This means a simple <strong>RegExp</strong> will over-find the piece of the <strong>String</strong> that is of interest, so the solution is to write a capture group <code>(pattern)</code>. The captured data can then be referenced as...</p> <ul><li>String replacement <code>"$n"</code> where <code>n</code> is the <strong>n th</strong> capture group (starting from <code>1</code>)</li> <li>The <strong>n th</strong> argument in a callback function</li> <li>If the <strong>RegExp</strong> is not flagged <code>g</code>, the <strong>n+1 th</strong> item in a returned <code>str.match</code> <strong>Array</strong></li> <li>If the <strong>RegExp</strong> is flagged <code>g</code>, <code>str.match</code> discards captures, use <code>re.exec</code> instead</li></ul> <p>Say there is a <strong>String</strong> where all <code>+</code> signs need to be replaced with a space, but only if they follow a letter character. This means a simple match would include that letter character and it would also be removed. Capturing it is the solution as it means the matched letter can be preserved.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">"aa+b+cc+1+2"</span><span class="token punctuation">,</span>
re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">([a-z])\+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
<span class="token comment">// String replacement</span>
str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token string">'$1 '</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// "aa b cc 1+2"</span>
<span class="token comment">// Function replacement</span>
str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">m<span class="token punctuation">,</span> $<span class="token number">1</span></span><span class="token punctuation">)</span> <span class="token operator">=></span> $<span class="token number">1</span> <span class="token operator">+</span> <span class="token string">' '</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// "aa b cc 1+2"</span>
</code></pre></div><h3 id="non-capture"><a href="#non-capture" class="header-anchor">#</a> Non-Capture</h3> <p>Using the form <code>(?:pattern)</code>, these work in a similar way to capture groups, except they do not store the contents of the group after the match.</p> <p>They can be particularly useful if other data is being captured which you don't want to move the indices of, but need to do some advanced pattern matching such as an OR</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">"aa+b+cc+1+2"</span><span class="token punctuation">,</span>
re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?:\b|c)([a-z])\+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token string">'$1 '</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// "aa+b c 1+2"</span>
</code></pre></div><h3 id="look-ahead"><a href="#look-ahead" class="header-anchor">#</a> Look-Ahead</h3> <p>If the desired match relies on something which follows it, rather than matching that and capturing it, it is possible to use a look-ahead to test for it but not include it in the match. A positive look-ahead has the form <code>(?=pattern)</code>, a negative look-ahead (where the expression match only happens if the look-ahead pattern did not match) has the form <code>(?!pattern)</code></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">"aa+b+cc+1+2"</span><span class="token punctuation">,</span>
re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\+(?=[a-z])</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token string">' '</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// "aa b cc+1+2"</span>
</code></pre></div><h2 id="replacing-string-match-with-a-callback-function"><a href="#replacing-string-match-with-a-callback-function" class="header-anchor">#</a> Replacing string match with a callback function</h2> <p><code>String#replace</code> can have a function as its second argument so you can provide a replacement based on some logic.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token string">"Some string Some"</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">Some</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">match<span class="token punctuation">,</span> startIndex<span class="token punctuation">,</span> wholeString</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>startIndex <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token string">'Start'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token string">'End'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// will return Start string End</span>
</code></pre></div><p>One line template library</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token operator">:</span> <span class="token string">'John'</span><span class="token punctuation">,</span> surname<span class="token operator">:</span> <span class="token string">'Doe'</span><span class="token punctuation">}</span>
<span class="token string">"My name is {surname}, {name} {surname}"</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?:{(.+?)})</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token parameter">x</span> <span class="token operator">=></span> data<span class="token punctuation">[</span>x<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// "My name is Doe, John Doe"</span>
</code></pre></div><h2 id="using-regex-exec-with-parentheses-regex-to-extract-matches-of-a-string"><a href="#using-regex-exec-with-parentheses-regex-to-extract-matches-of-a-string" class="header-anchor">#</a> Using Regex.exec() with parentheses regex to extract matches of a string</h2> <p>Sometimes you doesn't want to simply replace or remove the string. Sometimes you want to extract and process matches. Here an example of how you manipulate matches.</p> <p>What is a match ? When a compatible substring is found for the entire regex in the string, the exec command produce a match. A match is an array compose by firstly the whole substring that matched and all the parenthesis in the match.</p> <p>Imagine a html string :</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token operator"><</span>html<span class="token operator">></span>
<span class="token operator"><</span>head<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>head<span class="token operator">></span>
<span class="token operator"><</span>body<span class="token operator">></span>
<span class="token operator"><</span>h1<span class="token operator">></span>Example<span class="token operator"><</span><span class="token operator">/</span>h1<span class="token operator">></span>
<span class="token operator"><</span>p<span class="token operator">></span>Look a <span class="token keyword">this</span> great link <span class="token operator">:</span> <span class="token operator"><</span>a href<span class="token operator">=</span><span class="token string">"https://stackoverflow.com"</span><span class="token operator">></span>Stackoverflow<span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span> http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>anotherlinkoutsideatag<span class="token operator"><</span><span class="token operator">/</span>p<span class="token operator">></span>
Copyright <span class="token operator"><</span>a href<span class="token operator">=</span><span class="token string">"https://stackoverflow.com"</span><span class="token operator">></span>Stackoverflow<span class="token operator"><</span><span class="token operator">/</span>a<span class="token operator">></span>
<span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span>
</code></pre></div><p>You want to extract and get all the links inside an <code>a</code> tag. At first, here the regex you write :</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex"><a[^>]*href="https?:\/\/.*"[^>]*>[^<]*<\/a></span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
</code></pre></div><p>But now, imagine you want the <code>href</code> and the <code>anchor</code> of each link. And you want it together.
You can simply add a new regex in for each match <strong>OR</strong> you can use parentheses :</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex"><a[^>]*href="(https?:\/\/.*)"[^>]*>([^<]*)<\/a></span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
<span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'<html>\n <head></head>\n <body>\n <h1>Example</h1>\n <p>Look a this great link : <a href="https://stackoverflow.com">Stackoverflow</a> http://anotherlinkoutsideatag</p>\n\n Copyright <a href="https://stackoverflow.com">Stackoverflow</a>\n </body>\';\n'</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> m<span class="token punctuation">;</span>
<span class="token keyword">var</span> links <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>m <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>m<span class="token punctuation">.</span>index <span class="token operator">===</span> re<span class="token punctuation">.</span>lastIndex<span class="token punctuation">)</span> <span class="token punctuation">{</span>
re<span class="token punctuation">.</span>lastIndex<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// The all substring</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// The href subpart</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// The anchor subpart</span>
links<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
match <span class="token operator">:</span> m<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// the entire match</span>
href <span class="token operator">:</span> m<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// the first parenthesis => (https?:\/\/.*)</span>
anchor <span class="token operator">:</span> m<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// the second one => ([^<]*)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>At the end of the loop, you have an array of link with <code>anchor</code> and <code>href</code> and you can use it to write markdown for example :</p> <div class="language-js extra-class"><pre class="language-js"><code>links<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">link</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'[%s](%s)'</span><span class="token punctuation">,</span> link<span class="token punctuation">.</span>anchor<span class="token punctuation">,</span> link<span class="token punctuation">.</span>href<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>To go further :</p> <ul><li>Nested parenthesis</li></ul> <h4 id="syntax"><a href="#syntax" class="header-anchor">#</a> Syntax</h4> <ul><li>let regex = /<strong>pattern</strong>/[<strong>flags</strong>]</li> <li>let regex = new RegExp('<strong>pattern</strong>', [<strong>flags</strong>])</li> <li>let ismatch = regex.test('<strong>text</strong>')</li> <li>let results = regex.exec('<strong>text</strong>')</li></ul> <h4 id="parameters"><a href="#parameters" class="header-anchor">#</a> Parameters</h4> <table><thead><tr><th>Flags</th> <th>Details</th></tr></thead> <tbody><tr><td>g</td> <td><strong>g</strong>lobal. All matches (don't return on the first match).</td></tr> <tr><td>m</td> <td><strong>m</strong>ulti-line. Causes <strong>^</strong> & <strong>$</strong> to match the begin/end of each line (not only begin/end of string).</td></tr> <tr><td>i</td> <td><strong>i</strong>nsensitive. Case insensitive match (ignores case of [a-zA-Z]).</td></tr> <tr><td>u</td> <td><strong>u</strong>nicode : Pattern strings are treated as <strong>UTF-16</strong>. Also causes escape sequences to match Unicode characters.</td></tr> <tr><td>y</td> <td>stick<strong>y</strong>: matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes).</td></tr></tbody></table> <h4 id="remarks"><a href="#remarks" class="header-anchor">#</a> Remarks</h4> <p>The RegExp object is only as useful as your knowledge of Regular Expressions is strong. <a href="http://stackoverflow.com/questions/4736/learning-regular-expressions" target="_blank" rel="noopener noreferrer">See here<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> for an introductory primer, or see <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" target="_blank" rel="noopener noreferrer">MDN<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> for a more in-depth explanation.</p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/javascript/regular-expressions.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/javascript/intervals-and-timeouts.html" class="prev">
Intervals and Timeouts
</a></span> <span class="next"><a href="/javascript/cookies.html">
Cookies
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/1775.86753daa.js" defer></script>
</body>
</html>