Regular expression notes

Regular expression notes

Get Started

There are two ways to create a regular expression in JS:

  • Use a regular expression literal, which consists of patterns enclosed between slashes, as shown below:

    var re = /ab+c/ ; copy the code
  • Or through

    RegExp
    Constructor,
    new
    A regular expression:

    const REG = new new the RegExp ( 'ab & + C' ); duplicated code
  • Use regular expressions

    const reg = new RegExp ( 'abc' ); const str = 'helloabcword' ; reg.test(str); reg.exec(str); str.match(reg); str.matchAll(reg); str.replace(reg, '' ); str.search(reg); str.split(reg); Copy code

Simple mode

Simple mode is to directly find the structure you want to match.

/abc/
Can match
helloabc1231abckj
All in the string
abc
, The matching requires the same characters and their order, that is
/abc/
Can only match
abc
, Cannot match
bca
,
bac
.

const reg = /abc/ ; //Check whether a string matches a regular reg.test( 'abc' ); //true reg.test( 'abcdef' ); //true reg.test( '12313abcdef' ); //true reg. Test ( '12313ab1cdef' ); //to false copy the code

Special characters

Simply matching a certain fixed character structure has limited functions that can be achieved. Therefore, regular expressions also provide many special characters. With these special characters, rich matching can be achieved. According to its function, it can be divided into: character set , character class , group , quantifier , assertion . It should be noted that special characters already have special functions. When you want to match the special characters themselves, you need to escape, for example

\d
Time,
\d
Special characters need to be escaped, the regularity should be
/\\d/
.

character set

When multiple possibilities need to be matched, for example, to match

a
or
b
Characters, you can use pipes
|
Or character set
[]
.

  1. |
    And in JS
    ||
    Similar, which means one between two.
  2. []
    Represents the character set or range, the characters in the brackets, or the range of the character set described by the brackets will be matched.
    /a|b|c/
    or
    /[abc]/
    All match
    a
    or
    b
    or
    c
    , When the match is all numbers or all letters, do you need to list all the numbers, like this
    /[0123456789]/
    ?

In fact, there is a convenient way to write:

  • /[0-9]/
    versus
    /[0123456789]/
    The effect is consistent, matching all numbers.
  • /[az]/
    Match all lowercase letters.
  • /[AZ]/
    Match all uppercase letters.
  • /[a-zA-Z]/
    Match all letters.
  • /[a-zA-Z0-9]/
    Match all letters and numbers.
  • /[a-zA-Z0-9_]/
    Match alphanumeric and underscore, and
    /\w/
    The effect is consistent,
    \w
    It is a special character, which will be explained in detail below.
  • /[^a-zA-Z]/
    Matches non-alphabetic characters, here
    ^
    Represents not, that is, matches characters that are not contained in square brackets.

It should be noted that some special characters are no longer special characters in the character set, for example,

/[b|a]/
Means match
b
,
|
with
a
These three characters.

const str = 'Hi, your 6 apples is 10_$' ; //g represents the flag, that is, the entire string will be queried. If it is not added, the search will stop when the first matching character is found. The following will explain in detail const reg1 = /a|b|c|d/g ; console . log(str.match(reg1)); //['a'] const reg2 = /[abcd]/g ; console .log(str.match(reg2)); //['a'] const reg3 = /[0-9]/g ; console .log(str.match(reg3)); //['6', '1', '0'] const reg4 = /[az]/g ; console .log(str.match(reg4)); //['i','y','o','u','r','a', ' p','p','l','e','s','i','s'] const reg5 = /[AZ]/g ; console .log(str.match(reg5)); //['H'] const reg6 = /[0-9a-zA-Z_]/g ; console .log(str.match(reg6)); //['H','i','y','o','u' ,'r', '6','a','p','p','l','e','s','i','s', '1', '0', ' _']; const reg7 = /\w/g ; console .log(str.match(reg7)); //['H','i','y','o','u','r', '6 ','a','p','p','l','e','s','i','s', '1', '0','_']; const reg8 = /[^a-zA-Z]/g ; console .log(str.match(reg8)); //[',', '','', '6', '','', '', '1', '0', '_', '$'] duplicated code

Character class

Character classes are special characters that match a certain kind of characters.

.
Match any single character

.
Matches except line terminator (
\n
,
\r
,
\u2028
,
\u2029
), but in the character set, it only matches
.
, That is to say in the character set
.
No longer special characters.

const reg = /./g ; const str = '1aA./|`\n1a,' ; console .log(str.match(reg)); //['1','a','A', '/','|','`', '1','a',','] const reg1 = /\./g ; console .log(str.match(reg1)); //['.'] const REG2 = /[.]/g ; Console .log (str.match (REG2)); [ '.']// Copy the code

\d
Match a single digit

\d
Matches a single digit,
d
It is the abbreviation of digital, which is equivalent to
[0-9]
.

const str = 'zd1`2asd123' ; const reg = /\d/g ; const reg1 = /[0-9]/g ; console .log(str.match(reg)); //['1', '2', '1', '2', '3'] console .log(str.match(reg1)); //[ '1', '2', '1', '2', '3'] duplicated code

\D
Match a single non-digit

\D
Match a single non-digit character, and
[^0-9]
equivalence

const str = 'zd1`2asd123' ; const reg = /\D/g ; const reg1 = /[^0-9]/g ; console .log(str.match(reg)); //['z','d','`','a','s','d'] console .log(str.match(reg1)) ; //[ 'Z', 'D', ' `', 'A', 'S', 'D'] duplicated code

\w
Match single alphanumeric underscore

\w
Matches a single character digit underscore character,
w
Can be recorded as an abbreviation of word, and
[0-9a-zA-Z_]
equivalence

const str = 'zd1`As@_d)123' ; const reg =/\w/g ; const reg1 = /[0-9a-zA-Z_]/g ; console .log(str.match(reg)); //['z','d', '1','A','s','_','d', '1', '2' , '3'] console .log(str.match(reg1)); //['z','d', '1','A','s','_','d', '1 ', '2', '3' ] Copy code

\W
Match a single non-alphanumeric underscore

\W
Match a single non-numeric underscore character, and
[^0-9a-zA-Z_]
equivalence.

const str = 'zd1As@_d)123' ; const reg = /\W/g ; const reg1 = /[^0-9a-zA-Z_]/g ; Console .log (str.match (REG)); //[ '@', ')'] Console .log (str.match (REG1)); //[ '@', ')'] to copy the code

Non-printing characters

Non-printing characters can also be part of the regular. These non-printing characters include:

  • \cx
    Match by
    x
    A single control character specified, such as
    \cM
    Match one
    Control-M
    Or carriage return. See control characters for details .
  • \f
    Matches a single form feed, which is equivalent to
    \x0c
    with
    \cL
    .
  • \n
    Matches a single newline character, which is equivalent to
    \x0a
    with
    \cJ
    .
  • \r
    Matches a carriage return, which is equivalent to
    \x0d
    with
    \cM
    .
  • \t
    Matches a single tab character, which is equivalent to
    \x09
    with
    \cI
    .
  • \v
    Matches a vertical tab character, which is equivalent to
    \x0b
    with
    \cK
    .
const str = ` ` ; const str1 = '\t' ; const reg = /\n/g ; const reg1 = /\t/g ; Console .log (str.match (REG)); //[ '/n-', '/n-'] Console .log (str1.match (REG1)); //[ '/T'] duplicated code

\s
Match spaces

\s
Match a single space character. Space characters include space, tab, form feed, line feed, carriage return and Unicode blank characters, which are equivalent to
[\f\n\r\t\vu00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
.

const str = ` ` ; const str1 = '\t\v12\r\n' ; const reg = /\s/g ; console .log(str.match(reg)); //['\n','\n'] console .log(str1.match(reg)); //['\t','\u000b', '\r','\n'] console .log( '\v' .charCodeAt( 0 ).toString( 16 )); // bCopy code

\S
Match non-space

\S
Matches a single non-space character, equivalent to
[^\f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
.

const str1 = '\t\v12\r\n' ; const reg = /\S/g ; Console .log (str1.match (REG)); //[ '. 1', '2'] duplicated code

\
character

\
Characters have two meanings according to the characters that follow:

  • When it is followed by an ordinary character, it may form a special character together with the following ordinary character, such as
    \d
    ,
    \
    versus
    d
    Together they form the numeric special characters.
  • When it is followed by a special character, the following special character will be escaped to turn it into a normal character, such as
    \*
    ,
    \
    will
    *
    This special character is escaped to make it a normal character, matching
    *
    This symbol.
const str = 'aas(das*[\\' ; const reg = /\*/g ; const reg1 = /\w/g ; const reg2 = /\\/g ; console .log(str.match(reg)); //['*'] console .log(str.match(reg1)); //['a','a','s','d', 'A', 'S'] Console .log (str.match (REG2)); //[ '\\'] duplicated code

Capture group and non-capturing group

What should you do when you want to match one of two strings (for example, to match hello or word strings)? Capture group can be used at this time

(x)
Or non-capturing group
(?:x)
.

  • The difference between capturing group and non-capturing group

    The difference between the two is that the capturing group will remember the group matching items, causing performance loss. When you do not need to get the matching content in the group, you should use the non-capturing group as much as possible.

    const str = 'My first program: Hello World' ; const reg1 = /(Hello)|(World)/g ; console .log(str.match(reg1)); //['Hello','World'] const reg2 = /(?: the Hello) | (:? World)/G ; Console .log (str.match (reg2)); //[ 'the Hello', 'World'] Copy the code

    From the above results, it seems that there is no difference between the capturing group and the non-capturing group? This is because using

    g
    The mark of global match, the match function will not return the capture group information when global match, you can use
    matchAll
    Substitute. Let's try it in the case of non-global matching:

    const reg3 = /(Hello) (World)/ ; console .log(str.match(reg3)); //['Hello World','Hello','World',(index: 19),(input: ' My first program: Hello World'),(groups: undefined)]; const reg4 = /(?:Hello) (?:World)/ ; console .log(str.match(reg4)); //['Hello World',index: 19,input:'My first program: Hello World' , groups: undefined] copy the code

    can be seen

    reg3
    Return the complete matching string
    Hello World
    , Also returned the capture group
    Hello
    with
    World
    These two substrings, and
    reg4
    Only the complete string is returned.

  • Named capture group

    When there are multiple capture groups, and the capture groups are still nested,

    match
    The returned results are in their order, so it is very inconvenient to take the matching results of a certain group. At this time, you can use named capture groups
    (?<name>)
    . Look at the following example:

    //Take out the QQ number in front of the QQ mailbox, you can use zero-width assertion, here use the capture group const str2 = '112123232@qq.com' ; const reg5 = /(.+)(@(.+))/ ; console . log(str2.match(reg5)); //['112123232@qq.com','112123232','@qq.com','qq.com',index: 0,input: '112123232@qq.com ',groups: undefined] const reg6 = /(?<code>.+)(?<suffix>@(?<domain>.+))/ ; console .log(str2.match(reg6).groups); //{code: '112123232 ', domain:'qq.com', suffix:'@qq.com' } Copy code
  • Group match result backreference

    If there is a string

    <div>div value<div><span>span value<span>
    ,The start tag and the end tag are the same, the middle is its content, how to take it out
    div value
    with
    the value of span
    These two values need to be used at this time
    \n
    Special characters, where
    n
    Is an integer greater than 0,
    \n
    Will back-reference the first
    n
    For the results of matching groups, let s see an example:

    const str = '<div>12313<div><span>789<span>' ; const reg = /(<(\w+)>)(.+)\1/g ; ///1 refers to the first group <(\w+)> matching content, //when matching <div>12313<div>,/(<(\w+)>)(.+)\1/becomes <div>(.+) <div> //While matching <span>789<span>,/(<(\w+)>)(.+)\1/will become <span>(.+)<span> let res; while ( (res = reg.exec(str))) { console .log(res); } //['<div>12313<div>','<div>','div', '12313']; //['<span>789<span>','<span>','span' , '789']; Copy code

quantifier

Both the character set, character class and group mentioned above match a single number. When we need to match multiple items (character set, character class or group), for example, what should we do when we want to match multiple consecutive numbers at once ? This requires the use of quantifiers, which are used to indicate the number of characters or expressions to be matched.

*
Match any number of characters

*
Character means to match any number of characters or expressions.

const reg = /a*/g ; //When matching 1, there are 0 numbers of a, so it returns ``, the same is true when matching 2 console .log( '12aa45aa' .match (reg)); //['' ,'','aa','','','aa',''] const reg1 = /\w*/g ; console .log( 'ui1213m' .match(reg1)); //['ui1213m',''] const reg2 = /\d*/g ; console .log( 'a1231ad12' .match (reg2)); //['', '1231','','', '12',''] const reg3 = /(\$\d)*/g ; //matches $+ a number, and matches any number of times, like $1; console .log( '1$1$3' .match(reg3)); //['','$1$3',''] const REG4 = /[1-5a] */G ; Console .log ( 'yu8912a1234' .match (REG4)); //[ '', '', '', '', '12a1234', ''] Replication Code

To

12aa45aa
To match
/a*/g
For example, from the first character
1
Start matching, match 0
a
, Returns a null character, until
aa
, Match two
a
.

+
Match one & more

+
Match one or more characters or expressions.

const reg = /a+/g ; console .log( '12aa45aa' .match (reg)); //['aa','aa'] const reg1 = /\w+/g ; console .log( 'ui1213m' .match(reg1)); //['ui1213m'] const reg2 = /\d+/g ; console .log( 'a1231ad12' .match (reg2)); //['1231', '12'] const REG4 = /[1-5a] +/G ; Console .log ( 'yu8912a1234' .match (REG4)); //[ '12a1234'] duplicated code

?
Match one & below

?
The character matches 0 or 1 character or expression.

const reg = /a?/g ; console .log( '12aa45aa' .match (reg)); //['','','a','a','','','a', 'a',''] const reg1 = /\w?/g ; console .log( 'ui1213m' .match (reg1)); //['u','i', '1', '2', '1', '3' ,'m',''] const reg2 = /\d?/g ; console .log( 'a1231ad12' .match (reg2)); //['', '1', '2', '3', '1','', ' ', '1', '2',''] const reg4 = /[1-5a]?/g ; console .log( 'yu8912a1234' .match (reg4)); //['','','','', '1', '2', 'a', '1', '2', '3', '4', ''] duplicated code

{n}
Match n

{n}
Means match
n
Characters or expressions.

const reg = /a{2}/g ; console .log( '12aa45aa' .match (reg)); //['aa','aa'] const reg1 = /\w{3}/g ; console .log( 'ui1213m' .match(reg1)); //['ui1', '213'] const reg2 = /\d{3}/g ; console .log( 'a1231ad12' .match (reg2)); //['123'] const REG4 = /[1-5a] {}. 4/G ; Console .log ( 'yu8912a1234' .match (REG4)); //[ '12a1'] duplicated code

{n,}
Match more than n

{n,}
Indicates that at least a character or expression is matched
n
Times.

const reg = /a{2,}/g ; console .log( '12aa45aa' .match (reg)); //['aa','aa'] const reg1 = /\w{3,}/g ; console .log( 'ui1213m' .match(reg1)); //['ui1213m'] const reg2 = /\d{3,}/g ; console .log( 'a1231ad12' .match (reg2)); //['1231'] const REG4 = /[1-5a]. 4 {,}/G ; Console .log ( 'yu8912a1234' .match (REG4)); //[ '12a1234'] duplicated code

{n,m}
Match nm

{n,m}
Means to match a character or expression nm times, n is an integer greater than or equal to 0, and m is an integer greater than 0.

const reg = /a{1,5}/g ; console .log( '12aa45aa' .match (reg)); //['aa','aa'] const reg1 = /\w{3,5}/g ; console .log( 'ui1213m' .match(reg1)); //['ui121'] const reg2 = /\d{1,5}/g ; console .log( 'a1231ad12' .match (reg2)); //['1231', '12'] const REG4 = /[1-5a] {4,9}/G ; Console .log ( 'yu8912a1234' .match (REG4)); //[ '12a1234'] duplicated code

Non-greedy mode

The quantifiers introduced above are all "greedy", which means that it will match as many characters as possible, such as

"aaaaaaaa".match(/a{2,5}/)
, The result will be returned
['aaaaa']
, Regularity allows to match 2-5 a, it will match as many a as possible when the regularity is satisfied, but in other cases we need to make it non-greedy, let it enter the next match after satisfying the regularity, this Circumstances can be added after the quantifier
?
, Which means to perform non-greedy matching.

console .log( 'aa45aa' .match ( /a*/g )); //['aa','','','aa',''] console .log( 'aa4aa' .match ( /a *?/g )); //['','','','','',''] console .log( 'ui1213m' .match ( /\w+/g )); //['ui1213m'] console .log( 'ui1213m' .match ( /\w+?/g )); //['u' ,'i', '1', '2', '1', '3','m'] console .log( '1a12' .match ( /\d?/g )); //['1','', '1', '2',''] console .log( '1a12' .match ( /\d??/g )); //['','','','',''] console .log( 'helloo' .match ( /o{2}/g )); //['oo'] console .log( 'helloo' .match ( /o{2}?/g )); //['oo'] console .log( 'aaaaaaaa' .match( /a{2,5}/g )); //['aaaaa','aaa'] console .log( 'aaaaaaaa' .match( /a{2,5} ?/G )); //[ 'AA', 'AA', 'AA', 'AA'] duplicated code

Flags

The previous article will often add one after the regular when testing

g
,such as
/a{2}/g
, Then this
g
What does it mean? This one
g
It is the parameter of the regular expression, which means global search, that is, the entire string will be searched completely, instead of finding the first substring that meets the requirements and stopping the match. apart from
g
, The regular expression also provides another 5 optional parameters (flags) to allow global and case-insensitive search, etc. These parameters can be used alone or together in any order, and are included in the regular expression example.

Signdescription
gGlobal search.
iSearch case-insensitively.
mMulti-line search, ^ can also match the beginning of a newline character, and $ can also match the end of a line character
sAllow. Match newline characters.
uUse the unicode code pattern to match.
yPerform a "sticky" search, and the match starts from the current position of the target string.
console .log( 'abcABC' .match( /abc/g )); //['abc'] console .log( 'abcABC' .match( /abc/gi )); //['abc','ABC '] console .log( 'abc\nABC' .match ( /^\w+$/g )); //null console .log( 'abc\nABC' .match ( /^\w+$/gm )); //[ 'abc','ABC' ] copy code

Zero-width assertion

One of the components of the zero-width assertion is the boundary, which matches the boundary of the text, word or pattern, and it does not match anything by itself, namely

0
width.

^
Match the beginning of the string

Need to be used when you need to judge whether a string starts with a certain letter

^
, It should be noted that in the multi-line matching mode (that is, flags is m),
^
It also matches after a newline character.

let fruits = [ 'Apple' , 'Watermelon' , 'Orange' , 'Avocado' , 'Strawberry' ]; //Use regular/^A/to filter out fruits starting with'A '. let fruitsStartsWithA = fruits.filter( ( fruit ) => /^A/ .test(fruit)); console .log(fruitsStartsWithA); //['Apple','Avocado'] //Use regular/^[^A]/to select fruits that do not start with'A' //1) The first ^ matches the beginning of the input //2) The ^ in the character set means'not', [^A ] Means to match characters that are not'A ' let fruitsStartsWithNotA = fruits.filter( ( fruit ) => /^[^A]/ .test(fruit)); console .log(fruitsStartsWithNotA); //['Watermelon', 'Orange','Strawberry'] const str = 'hello world\nhello my world' ; console .log(str.match( /^hello/g )); //['hello'] //In multi-line mode, ^ also matches the end of the newline character console . log (str.match ( /^ Hello/GM )); //[ 'Hello', 'Hello'] duplicated code

$
Match the end of the string

$
Used to determine whether a string ends with a string, and
^
Similarly, in multi-line mode,
$
Also matches the beginning of a newline character.

let fruits = [ 'Apple' , 'Watermelon' , 'Orange' , 'Avocado' , 'Strawberry' ]; //Use regular/o$/to filter out fruits ending with'o '. let fruitsStartsWithA = fruits.filter( ( fruit ) => /o$/ .test(fruit)); console .log(fruitsStartsWithA); //['Avocado'] //Filter out fruits that do not end with'o ' let fruitsStartsWithNotA = fruits.filter( ( fruit ) => /[^o]$/ .test(fruit)); console .log(fruitsStartsWithNotA); //['Apple ','Watermelon','Orange','Strawberry'] const str = 'hello world\nhello my world' ; console .log(str.match( /world$/g )); //['world'] //In multi-line mode, $ also matches the beginning of a newline character console . log(str.match( /world$/gm )); //['world','world'] //Filter out fruit consoles that both start with A and end with o .log(fruits.filter( ( fruit ) => /^A\w*o$/ .test(fruit))); //['Avocado '] Copy code

\b
Match word boundaries

Match the left and right boundaries of a word, which is a position before or after another character, such as between a letter and a space.

  • /\bm/
    Matches "m" in "moon"
  • /oo\b/
    "Oo" will not be matched in "moon", because "oo" is followed by the word character "n".
  • /oon\b/
    Match "oon" in "moon", because "oon" is the end of the string, so there is no word character after it
  • /\w\b\w/
    Will never match anything, because there will never be non-word characters and word characters after a word character
let fruitsWithDescription = [ 'Red apple' , 'Orange orange' , 'Green Avocado' ]; //Select descriptions that contain words ending in "en" or "ed": let enEdSelection = fruitsWithDescription.filter( ( descr ) => /(en|ed)\b/ .test(descr)); Console .log (enEdSelection); //[ 'Apple Red', 'Green Avocado'] duplicated code

\B
Match word non-boundary

Match a non-word boundary, which is the position where the previous character and the next character belong to the same type (letters and numbers are the same type, letters and spaces are different types): either both must be words, or both must be non-words , Such as between two letters or between two spaces.

let values = [ 'Red apple' , 'ppOrange orangepp' , 'Greenpp Avocado' ]; // pp const result = values.filter((val) => /\Bpp\B/.test(val)); console.log(result); //[ 'Red apple' ] const str = 'hello1 word llo 1231'; console.log(str.match(/llo/g)); //[ 'llo', 'llo' ] console.log(str.match(/llo\B/g)); //[ 'llo' ]

x(?=y)

x(?=y)
x
y
x
x
.

// First test First let regex = /First(?= test)/g; console.log('First test'.match(regex)); //[ 'First' ] //First test null console.log('test First peach'.match(regex)); //null console.log('This is a First test in a year.'.match(regex)); //[ 'First' ] console.log('This is a First peach in a month.'.match(regex)); //null

x(?!y)

x(?!y)
x
y
x
x
.

// First test First let regex = /First(?! test)/g; //First test , null console.log('First test'.match(regex)); //null //First test First console.log('test First peach'.match(regex)); //[ 'First' ] console.log('This is a First test in a year.'.match(regex)); //null console.log('This is a First peach in a month.'.match(regex)); //[ 'First' ]

(?<=y)x

(?<=y)x
x
y
x
x
x
y

let oranges = ['69 ripe orange z ', 'green orange B', '78 ripe orange A ']; // '69 ripe orange A ' '78 ripe orange A ' const result = oranges.filter((fruit) => /(?<=ripe\s)orange/.test(fruit)); ///(?<=ripe\s)orange/ ripe\s orange console.log(result); //[ '69 ripe orange z ', '78 ripe orange A ' ] //vue {{name}}, name const str = ' {{name}} {{age}}'; // {{, }} console.log(str.match(/(?<={{)\w+(?=}})/g)); //[ 'name', 'age' ]

(?<!y)x

(?<!y)x
x
y
x
.

let oranges = ['69 ripe orange z ', '99 green orange B', '78 ripe orange A ']; // '99 green orange B' const result = oranges.filter((fruit) => /(?<!ripe\s)orange/.test(fruit)); ///(?<!ripe\s)orange/ orange ripe\s orange //'99 green orange B' console.log(result); //[ '99 green orange B' ] //< > const str = '<d>hello<d>'; console.log(str.match(/(?<!<)\w+(?!>)/g)); //[ 'hello' ]

safari

JS

JS

RegExp
test
exec
String
match
matchAll
replace
search
split

RegExp

RegExp.prototype.test()

test
test
true

  • regexObj.test(str)

  • str

  • true false

console.log(/\d+/.test(' 1213')); //true console.log(/^\d+/.test(' 1213')); //false console.log(/^\d+/.test('1213&')); //true console.log(/^\d+$/.test('1213&')); //false console.log(/^\d+$/.test('1213')); //true const str = 'hello world'; const reg = /hello/; reg.test(str); //true

RegExp.prototype.exec()

exec
null
global
sticky
/foo/g
or
/foo/y
JavaScript RegExp
lastIndex
exec()
.

  • regexObj.exec(str)

  • str

  • exec() index input lastIndex

    exec() null lastIndex 0

// while/g const regex = /foo*/g; const str = 'table football, foosball'; let result; while ((result = regex.exec(str)) !== null) { console.log(` ${result[0]} ${regex.lastIndex} `); } // foo 9 // foo 19 var re = /quick\s(brown).+?(jumps)/i; var result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog'); console.log(result); //[ 'Quick Brown Fox Jumps','Brown','Jumps',index: 4,input: 'The Quick Brown Fox Jumps Over The Lazy Dog',groups: undefined ]

String

String.prototype.match()

match

  • str.match(regexp)

  • regexp new RegExp(obj) RegExp match() Array [""]

  • g g Array

const str = 'https://www.baidu.com http://www.google.com'; console.log(str.match(/(https?):\/\//)); //[ 'https://','https',index: 0] console.log(str.match(/(https?):\/\//g)); //[ 'https://', 'http://' ]

String.prototype.matchAll()

matchAll()

  • str.matchAll(regexp)

  • regexp new RegExp(obj) RegExp

    RegExp g TypeError

const regexp = /t(e)(st(\d?))/g; const str = 'test1test2'; const array = [...str.matchAll(regexp)]; console.log(array[0]); //expected output: Array ["test1", "e", "st1", "1"] console.log(array[1]); //expected output: Array ["test2", "e", "st2", "2"]

String.prototype.replace

replace()

  • str.replace(regexp|substr, newSubStr|function)

  • regexp (pattern) RegExp

    substr (pattern) newSubStr

    newSubStr (replacement)

    function (replacement)

const mounth1 = 'Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec'; console.log(mounth1.replace(';', ',')); //Jan,Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec console.log(mounth1.replace(';', (res) => res + ',')); //Jan;,Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec const month = 'Jan<Feb,Mar>Apr,May<Jun>Jul,Aug|Sep[Oct]Nov,Dec'; console.log(month.replace(/[<,>|[\]]/g, ';')); //Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec console.log(month.replace(/[<,>|[\]]/g, (res) => `{{${res}}}`)); //Jan{{<}}Feb{{,}}Mar{{>}}Apr{{,}}May{{<}}Jun{{>}}Jul{{,}}Aug{{|}}Sep{{[}}Oct{{]}}Nov{{,}}Dec

replace

  • $$
    "$"
  • $&
  • $
    `
  • $'
  • $n
    RegExp n 100 n 1 n 3 $3
  • $<Name>
    Name
var re = /(\w+)\s(\w+)/; var str = 'John Smith'; var newstr = str.replace(re, '$2, $1'); //Smith, John console.log(newstr);

String.prototype.search

search()
String , search() ; -1

const str = 'hey JudE'; const re = /[A-Z]/g; const re2 = /[.]/g; console.log(str.search(re)); //4 console.log(str.search(re2)); //-1

String.prototype.split

split()
String

const month = 'Jan<Feb,Mar>Apr,May<Jun>Jul,Aug|Sep[Oct]Nov,Dec'; console.log(line.split(/\d+/)); //[ 'Oh', 'brave', 'new', 'world.' ] console.log(month.split(/[<,>|[\]]/)); //[ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep', 'Oct','Nov','Dec' ]

MDN