1. Re objectsRegExp
Common properties and methods on
1.1 RegExp
The properties of the
1.1.1 Read-only Properties:RegExp.prototype.global
, RegExp.prototype.ignoreCase
, RegExp.prototype.multiline
The value true or false indicates whether the regular expression uses the corresponding modifier
global
Describe whether to useg
The modifierignoreCase
Describe whether to usei
The modifiermultiline
Describe whether to usem
The modifier
1.1.2 regExpObj.lastIndex
(Readable and modifiable)
- There are only existence modifiers
g
When, or no matterlastIndex
The value of, defaults to0
)let reg = /\d+/ reg.lastIndex = 4 console.log(reg.lastIndex) / / = > 4 reg.exec("123a456") //=> ["123", index: 0, input: "123a456", groups: undefined] // Even if you change the value of lastIndex, it will still start at 0 when matching Copy the code
- Represents the subscript from the beginning of the string the next time the string is matched (whether or not it is the same string that was just matched)
- When the matching string fails, the value becomes
0
let reg = /\dabc/g let str = "1abc2abc3abc" console.log(reg.lastIndex) //=> 0 indicates that the next match starts at the subscript 0 of the string reg.exec(str) //=> ["1abc", index: 0, input: "1abc2abc3abc", groups: undefined] console.log(reg.lastIndex) //=> 4 Indicates that the next match starts at subscript 4 of the string reg.exec(str) //=> ["2abc", index: 4, input: "1abc2abc3abc", groups: undefined] // Replace the string matched by the re, and the match will still start at lastIndex console.log(reg.lastIndex) //=> 8 Indicates that the next match starts at subscript 8 of the string reg.exec("0abc12345abc") //=> ["5abc", index: 8, input: "0abc12345abc", groups: undefined] Copy the code
1.2 RegExp
The method of
1.2.1 RegExp.prototype.test(str)
Attempts to match the string STR with a re object, returning true if it matches, false if it does not
If the regular expression has the g modifier, it also has the ability to change lastIndex
/\d+/.test("12345") //=> true
/\d+/g.test("abcde") //=> false let reg = /\d+/g
console.log(reg.lastIndex) / / = > 0
reg.test("12abc") //=> true
console.log(reg.lastIndex) / / = > 2
Copy the code
1.2.2 RegExp.prototype.exec(str)
Try to match the string STR with a regular expression, or return null if it does not match,
If it matches, an array is returned
- The subscript
0
: indicates the matching result of the regular expression - The subscript
1, 2, 3...
: represents the result of capture - attribute
index
: indicates that the regular expression matches the substring in the original stringstr
The starting index of - attribute
input
: represents the source string to be matchedstr
- attribute
groups
: Named capture group- The most basic capture should be
(x)
To capture the matching regular expressionx
Regular string - But apply a named capture group:
(? <name>x)
If the corresponding string is captured, it will also capture the string withvalue
Stored in the form ofkey
Give the independent name toname
的groups
In the object
- The most basic capture should be
let reg = / (?
\d+)\.(?
\d+)/
let str = "123.456"
let array = reg.exec(str)
console.log(array)
/ / = > [" 123.456 ", "123", "456",
// index: 0, input: "123.456", groups: {integerPart: "123", decimalPart: "456"}]
/** * array[0] is the result of this regular match. Array [1, 2, 3...] Array. input is the source string matched by the regular expression. Array. groups is the object of the named capture group. Holds the named captured key-value pair */
Copy the code
2. String
A common method of regex on
2.1 String.prototype.match(reg)
- If the incoming
reg
Is notRegExp
Is converted toRegExp
- if
reg
There is nog
Modifier, which returns the value andRegExp.prototype.exec(str)
The same - if
reg
containsg
Modifier returns an array of the results of all re matches (noneindex
,input
和groups
, does not capture the resulting string), returns if it cannot matchnull
let str = "123.456"
str.match(/\d+/) //=> ["123", index: 0, input: "123.456", groups: undefined]
str.match("\\d+") //=> ["123", index: 0, input: "123.456", groups: undefined]
str.match(/\d+/g) / / = > [" 123 ", "456"]
str.match(/(\d+)\.(\d+)/g) / / = > (" 123.456 ")
Copy the code
2.2 String.prototype.matchAll(reg)
Fixed a bug where string.prototype. match(reg) could not get captured content
- If the incoming
reg
Is notRegExp
Is converted toRegExp
- if
reg
There is nog
Modifier, an error is reported directly
const str = "String.prototype.matchAll(reg)"
const reg = /(\w+)/g
console.log(... str.matchAll(reg))/* => * ["String", "String", index: 0, input: "String.prototype.matchAll(reg)", groups: undefined] * ["prototype", "prototype", index: 7, input: "String.prototype.matchAll(reg)", groups: undefined] * ["matchAll", "matchAll", index: 17, input: "String.prototype.matchAll(reg)", groups: undefined] * ["reg", "reg", index: 26, input: "String.prototype.matchAll(reg)", groups: Undefined] * each array is equivalent to the result of a reg.exec(STR) */
Copy the code
2.3 String.prototype.replace(reg|subStr, newSubStr|callbackFn)
(Does not change the source string)
Brief description: Match the first parameter and replace it with the second parameter
- The first parameter:
reg|subStr
: A regular expression or substring used to match substrings in the string being replaced- Regular expression if not
g
Modifier, andsubStr
Again, only the first matching string is replacedlet str = "123abc123" let resStr0 = str.replace(/\d+/."xxx") // Not a global match let resStr1 = str.replace(/\d+/g."xxx") // Global matching let resStr2 = str.replace("123"."xxx") console.log(resStr0, resStr1, resStr2) //=> "xxxabc123", "xxxabcxxx", "xxxabc123" Copy the code
- Regular expression if not
- The second parameter:
newSubStr|callbackFn
: applies to the result of each re matchnewSubStr
或callbackFn
Replace the return value ofnewSubStr
: has special uses (to insert special variable names into a string)-
$n: 100 > n >= 1, indicating the capture from 1
let str = "123.456".replace(/(\d+)\.(\d+)/."$2.$1") // This re matches the first set of traps "123" and the second set of traps "456" console.log(str) / / = > "456.123" Copy the code
-
$
: When you have a named capture group, you can use $+
to represent the capture
let str = "123.456" let reg = / (?
\d+)\.(? let newSubStr = "$<right>+$<left>" str.replace(reg, newSubStr) / / = > "456 + 123" Copy the code\d+)/ -
$& : indicates the substring to be matched by the second re
"123.456".replace(/\d+/g."0 $&") / / = > "0123.0456" /** * the result of the first re match is "123", so "$&" represents "123". Replace with "0123" * the result of the second re match is "456", so "$&" represents "456" * For the second match, replace with "0$&", which is "0456" */ Copy the code
-
$’ : $+ ‘(backquotes) represents the content to the left of the currently matched substring
$’ : $+ ‘(single quotes) represents the content to the right of the currently matched substring
let str = "123.456".replace(/ /. /."{left: $`, right: $'}") // The re matches the "." in the string and is replaced with "{left: $', right: $'}" console.log(str) //=> "123{left: 123, right: 456}456" Copy the code
-
callbackFn(subStr, $1, $2, ... , offset, str, groups)
: replaces the substring matched by the regular expressionsubStr
: Indicates the string to be matched by the current reAt $1, $2,...
: represents the string captured by the groupoffset
: indicates the starting index of the currently matched substring in the source stringstr
: Source string to be matchedgroups
: indicates the corresponding named capture group object if there is a named capture group, otherwise undefined
let str = "this is a string" let reg = /\b(\w{1})\w*\b/g str.replace(reg, (subStr, capture1, offset, str, groups) = > { console.log(subStr, capture1, offset, str, groups) //=> "this", "t", 0, "this is a string", undefined //=> "is", "i", 5, "this is a string", undefined //=> "a", "a", 8, "this is a string", undefined //=> "string", "s", 10, "this is a string", undefined return subStr + "233" }) //=> "this233 is233 a233 string233" // In the example above, a total of four matching substitutions are performed and the final result is returned Copy the code
2.4 String.prototype.search(reg)
Returns the starting subscript in the source string of the substring that matches the corresponding regular expression, or -1 if there is no match
let str = "abc123"
let reg = /\d+/
str.search(reg) / / = > 3
Copy the code
2.5 String.prototype.split([separator[, limit]])
(Does not modify the source string)
Separator is passed in as the separator, stores the split result into an array and returns the array. If there is a limit, the array contains at most a limit of elements
separator
:string
|reg
If no match is found, the entire string is used as an element of the array; ifseparator
Is an empty string, then each character in the source string is an element of the array
const str = "this is a string"
str.split() //=> ["this is a string"]
str.split(/\b/) //=> ["this", " ", "is", " ", "a", " ", "string"]
str.split("") //=> ["this", "is", "a", "string"]
str.split("".2) //=> ["this", "is"]
Copy the code