Update Groups_capturing.html

This commit is contained in:
Aakash Panchal
2020-03-08 15:33:48 +05:30
committed by GitHub
parent 23d1339a1f
commit 485b29c8f8

View File

@@ -1,3 +1,4 @@
<html> <html>
<head> <head>
<style type="text/css"> <style type="text/css">
@@ -50,7 +51,7 @@
<h2 id="groupscapturing">Groups &amp; Capturing</h2> <h2 id="groupscapturing">Groups &amp; Capturing</h2>
<p>Grouping is the most useful feature of regex. Grouping can be done by placing regular expression inside round brackets. </p> <p>Grouping is the most useful feature of regex. Grouping can be done by placing regular expression inside round brackets. In this article, we will see how to extract and replace data using groups. </p>
<p>It unifies the regular expressions inside it as a single unit. Let's look at its usages one by one:</p> <p>It unifies the regular expressions inside it as a single unit. Let's look at its usages one by one:</p>
@@ -71,16 +72,12 @@
<div class="container"> <div class="container">
<iframe scrolling="no" style="position: absolute; top: -9999em; visibility: hidden;" onload="this.style.position='static'; this.style.visibility='visible';" src="https://regexr.com/4vuag" class="embed"></iframe> <iframe scrolling="no" style="position: absolute; top: -9999em; visibility: hidden;" onload="this.style.position='static'; this.style.visibility='visible';" src="https://regexr.com/4vuag" class="embed"></iframe>
</div></li> </div>
</ol>
<p>Similarly, you can use other quantifiers.</p> <p>Similarly, you can use other quantifiers.</p>
<ol>
<li><p>To extract and replace substrings using groups. So, we call groups <strong>Capturing groups</strong>, becuase we are capturing data(substrings) using groups.</p> <li><p>To extract and replace substrings using groups. So, we call groups <strong>Capturing groups</strong>, becuase we are capturing data(substrings) using groups.</p>
<p>In this part, we will see how to extract and replace data using groups in Javascript.</p>
<p><strong>Data Extraction</strong></p> <p><strong>Data Extraction</strong></p>
<p>Observe the code below.</p> <p>Observe the code below.</p>
@@ -97,26 +94,32 @@
// printing // printing
console.log(result); console.log(result);
/* Output will be:
[
'2020-01-20', //-------pattern
'2020', //-----First group
'01', //-------Second group
'20', //-------Third group
index: 0,
input: '2020-01-20',
groups: undefined
]
*/
// Data extraction // Data extraction
console.log(result[1]); // First group console.log(result[1]); // First group
console.log(result[2]); // Second group console.log(result[2]); // Second group
console.log(result[3]); // Third group console.log(result[3]); // Third group
</code></pre> </code></pre>
The output will be:
<pre><code class="js language-js">[
'2020-01-20',
'2020',
'01',
'20',
index: 0,
input: '2020-01-20',
groups: undefined
]
2020
01
20
</code></pre>
<p>In the output array, the first data is a match string followed by the matched groups in the order.</p> <p>In the output array, the first data is a match string followed by the matched groups in the order.</p>
<p><strong>Data Replacement:</strong></p> <p><strong>Data Replacement</strong></p>
<p><code>Replace</code> is another function, which can be used to replace and rearrange the data using regex. Observe the code below.</p> <p><code>Replace</code> is another function, which can be used to replace and rearrange the data using regex. Observe the code below.</p>