js: Summary of common methods of regular expressions test, exec, match, matchAll, replace, replaceAll, search

regular use

Several commonly used methods are: test, exec, match, matchAll, replace, replaceAll, search

test

// 匹配返回true,不匹配false
/e/.test("The best things in life are free!")

match/matchAll

without g

Suppose there is a string:

const str = 'abc${a}dfdsd${b}ddd'

We want to get the value {}inside

Those wrapped in parentheses in the regex are capture groups

'abc${a}dfdsd${b}ddd'.match(/\{(.+?)\}/)

insert image description here
It can be seen that the 0th item of the array here is the matched value, and the 1st item is the capture group. Because the regular expression does not add g, only the first item is matched.

plus g

insert image description here
After adding g, you can indeed match all of them, but the capture group and other group, index and other information will not be displayed.

use of group

Define a variable in the one to be captured ?<var>, and the groups field of the returned result will have
insert image description here

matchAll

without g

insert image description here

plus g

insert image description here
What is returned is an iterator. Let’s structure it. We
insert image description here
can see that after adding g to match, we cannot get all the capture values, but matchAll can

Comparison between match and matchAll:
1. For match, if the regular expression is modified with g, the result returns a list of all strings that match the regular expression. Captured items are ignored!
2. For match, if there is no g modification in the regular expression, the result will not only return the first match, but also list all its captured items!
3. For matchAll, if the regular expression is modified with g, the iterated items returned are arrays one by one. In addition to the complete string matched and all captured items, the array also contains index, input, and groups. Three attributes, index indicates the index position of the current matching item in the original string, input indicates the input original string, and groups is an object containing all groups.
4. For matchAll, if the regular expression is not modified with g, the result information is exactly the same as that described in point 3. But its iterator has only one item, the first match.

exec

Consistent with the return result of match

without g

/\{(.+?)\}/.exec('abc${a}dfdsd${b}ddd')

insert image description here

plus g

insert image description here
It can be seen that it is the same as without adding g. How would you say that this API performs global retrieval?
insert image description here
insert image description here
In the global mode, exec can be executed multiple times, and the result returned by each execution is the same as the match function. And the lastIndex position will be recorded under the regular object, and null will be returned and the lastIndex record will be 0 if there is no match.
So according to this we can get the result we want by traversing:

// 返回的数组
let m = null;
while((m = reg.exec(str)) != null){
    
    
  // 每次匹配返回的结果
  console.log(m);
  // 每次匹配返回的lastIndex
  console.log(reg.lastIndex);
}
// 最终为0
console.log(reg.lastIndex);

insert image description here

search

insert image description here
Returns the index of the first occurrence of the regular expression in the string if the match is successful; otherwise, returns -1.
No distinction is made between glob patterns.

replace

// 返回值是新的字符串
const newStr = string.replace(searchvalue,newvalue)
"Mr Blue has a blue house and a blue car".replace(/blue/g, "red");

The second parameter newvalue of the replace function is special, it has the following special strings:

$$  直接量符号(就是当做'$$'字符用)
$&  与正则相匹配的字符串
$`  匹配字符串左边的字符 
$’  匹配字符串右边的字符
$1,$2,$,3,…,$n  匹配结果中对应的分组匹配结果

If you want to eliminate the influence of $, you can write it as the return value of the function. The function has the following parameters:

  • The first parameter: the matched string
  • The middle parameter: If the regex uses group matching, it is multiple, otherwise there is no such parameter
  • The penultimate parameter: the corresponding index position of the matching string
  • Last parameter: raw string

example

const value = '123-234-234-234'
const pattern = '-'
value.replace(pattern, (item, index) => {
    
    
	// item 替换元素,index 替换元素的下标
	console.log(item, index)
	return '/'
})

// 输出:
// - 3
// 123/234-234-234

The difference between replace and replaceAll?
When the first parameter is a string, replace only replaces the first matched position, and replaceAll replaces every matched position.
When the first arguments are both regular expressions, there is no difference.

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/130686728