mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-03-16 06:10:00 +00:00
Update named_groups.html
This commit is contained in:
parent
189245b75e
commit
ebf0e89270
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
@ -57,19 +58,19 @@
|
|||||||
|
|
||||||
<p>Below is a code, we have already looked in <strong>capturing groups</strong> part. You can see, the code is more readable now.</p>
|
<p>Below is a code, we have already looked in <strong>capturing groups</strong> part. You can see, the code is more readable now.</p>
|
||||||
|
|
||||||
<pre><code class="js language-js"> var str = "2020-01-20";
|
<pre><code class="js language-js">var str = "2020-01-20";
|
||||||
|
|
||||||
|
// Pattern string
|
||||||
|
var pattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;
|
||||||
|
|
||||||
// Pattern string
|
// ^ ^ ^
|
||||||
var pattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;
|
//group-no: 1 2 3
|
||||||
|
|
||||||
// ^ ^ ^
|
// Data replacement using $<group_name>
|
||||||
//group-no: 1 2 3
|
var ans=str.replace(pattern, '$<day>-$<month>-$<year>');
|
||||||
|
|
||||||
// Data replacement using $<group_name>
|
console.log(ans);
|
||||||
var ans=str.replace(pattern, '$<day>-$<month>-$<year>');
|
// Output will be: 20-01-2020
|
||||||
|
|
||||||
console.log(ans);
|
|
||||||
// Output will be: 20-01-2020
|
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
<p>Backreference syntax for numbered groups works for named capture groups as well. <code>\k<name></code> matches the string that was previously matched by the named capture group <code>name</code>, which is a standard way to backreference named group.</p>
|
<p>Backreference syntax for numbered groups works for named capture groups as well. <code>\k<name></code> matches the string that was previously matched by the named capture group <code>name</code>, which is a standard way to backreference named group.</p>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user