: (...). .
:
- .
- .
.
: gogogo
: go + : g : o . goooo gooooooooo.
: (go) + go gogo gogogo .
alert( 'Gogogo now!'.match(/(go)+/ig) ); // "Gogogo"
:
.
:
mail.com
users.mail.com
smith.users.mail.com
.
: (\ w + \.) + \ w +:
let regexp = /(\w+\.)+\w+/g;
alert( "site.com my.site.com".match(regexp) ); // site.com,my.site.com
my-site.com class: \ w.
\ w [\ w-] : ([\ w -] + \.) + \ w +.
:
. .
: name @ domain. . : [-. \ w] +.
:
let regexp = /[-.\w]+@([\w-]+\.)+[\w-]+/g;
alert("my@mail.com @ his@site.com.uk".match(regexp)); // my@mail.com, his@site.com.uk
. .
. .
str.match (regexp) regexp g :
-
0: . -
1: . -
2: .
HTML <. *> . ( ) .
: <(. *)>.
: <h1> : h1 :
let str = '<h1>Hello, world!</h1>';
let tag = str.match(/<(.*?)>/);
alert( tag[0] ); // <h1>
alert( tag[1] ); // h1
. .
: <span class =" my "> :
- :
span class =" my ". - :
span. - :
class =" my ".
: <(([a-z] +) \ s * ([^>] *))>.
( ):
In action:
let str = '<span class="my">';
let regexp = /<(([a-z]+)\s*([^>]*))>/;
let result = str.match(regexp);
alert(result[0]); // <span class="my">
alert(result[1]); // span class="my"
alert(result[2]); // span
alert(result[3]); // class="my"
.
. [1]. .
[2] `` : ([az] +) - [3] : : ([^>] * ) `.
:
( : (...)) .
regexp a (z) (c). z c ".
a :
let match = 'a'.match(/a(z)?(c)?/);
alert( match.length ); // 3
alert( match[0] ); // a (whole match)
alert( match[1] ); // undefined
alert( match[2] ); // undefined
3 .
ac:
let match = 'ac'.match(/a(z)?(c)?/)
alert( match.length ); // 3
alert( match[0] ); // ac (whole match)
alert( match[1] ); // undefined, because there's nothing for (z)?
alert( match[2] ); // c
: 3. 'pattern: (z) " ["ac" undefined "c"].
: matchAll
matchAll ( g) match .
:
let str = '<h1> <h2>';
let tags = str.match(/<(.*?)>/g);
alert( tags ); // <h1>,<h2>
. .
str.matchAll (regexp).
.
match 3 :
- .
- pattern: g .
- .
:
let results = '<h1> <h2>'.matchAll(/<(.*?)>/gi);
// results - is not an array, but an iterable object
alert(results); // [object RegExp String Iterator]
alert(results[0]); // undefined (*)
results = Array.from(results); // let's turn it into array
alert(results[0]); // <h1>,h1 (1st tag)
alert(results[1]); // <h2>,h2 (2nd tag)
(*). " [0] . Array Array.from`. <info: iterable>.
Array.from :
let results = '<h1> <h2>'.matchAll(/<(.*?)>/gi);
for(let result of results) {
alert(result);
// first alert: <h1>,h1
// second: <h2>,h2
}
destructuring:
let [tag1, tag2] = '<h1> <h2>'.matchAll(/<(.*?)>/gi);
matchAll : g: ( ) ( ):
let results = '<h1> <h2>'.matchAll(/<(.*?)>/gi);
let [tag1, tag2] = results;
alert( tag1[0] ); // <h1>
alert( tag1[1] ); // h1
alert( tag1.index ); // 0
alert( tag1.input ); // <h1> <h2>
.
matchAll . . .
.
100 for..of 5 " break ". 95 .
. . : .
"pattern : ` .
--:
let dateRegexp = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/;
let str = "2019-04-30";
let groups = str.match(dateRegexp).groups;
alert(groups.year); // 2019
alert(groups.month); // 04
alert(groups.day); // 30
.groups .
g.
matchAll :
let dateRegexp = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/g;
let str = "2019-10-30 2020-01-01";
let results = str.matchAll(dateRegexp);
for(let result of results) {
let {year, month, day} = result.groups;
alert(`${day}.${month}.${year}`);
// first alert: 30.10.2019
// second: 01.01.2020
}
str.replace (regexp ) regexp str replace. $ n n .
let str = "John Bull";
let regexp = /(\w+) (\w+)/;
alert( str.replace(regexp, '$2, $1') ); // Bull, John
$ <name>.
year-month-day day.month.year:
let regexp = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/g;
let str = "2019-10-30, 2020-01-01";
alert( str.replace(regexp, '$<day>.$<month>.$<year>') );
// 30.10.2019, 01.01.2020
:
.
pattern:: .
"pattern: (go) + (go) : pattern :( : go) + ` .
John :
let str = "Gogogo John!";
// ?: exludes 'go' from capturing
let regexp = /(?:go)+ (\w+)/i;
let result = str.match(regexp);
alert( result[0] ); // Gogogo John (full match)
alert( result[1] ); // John
alert( result.length ); // 2 (no more items in the array)
.
( <name> ...).
:
-
str.match" : g`. -
str.matchAll.
. .
str.replace: $ n $ <name>.
pattern:: . . .
<code><pre>10 (plnkr, JSBin, codepen)