Learn more about regular expressions

1. Capture and reference

Strings matched (captured) by regular expressions will be temporarily stored. Among them, the strings captured by the group will be numbered from 1, so we can refer to these strings:

var reg = /(\d{4})-(\d{2})-(\d{2})/
var date = '2021-08-29'
reg.test(date)
// 捕获之前要先test/exec
console.log(RegExp.$1); //2021
console.log(RegExp.$2); //08
console.log(RegExp.$3); //29

$1 refers to the first captured string, $2 the second, and so on.

1.1 Capture of nested groups

//遇到嵌套的分组是捕获的顺序
var reg = /((apple) is (a (fruit)))/
var str = "apple is a fruit"
reg.test(str) // true
RegExp.$1 // apple is a fruit
RegExp.$2 // apple
RegExp.$3 // a fruit
RegExp.$4 // fruit
//由结果可知:捕获的规则从左至右,以左括号'('出现的顺序进行捕获

1.2 Citation

References in regular expressions are called backreferences

var reg = /(\w{3}) is \1/; 
console.log(reg.test('kid is kid')); // true
console.log(reg.test('dik is dik')); // true
console.log(reg.test('kid is dik')); // false
console.log(reg.test('dik is kid')); // false
//这里'\1'引用了第一个被分组捕获的字符串,由此可看出表达式是动态决定的

// '\1'编号不能越界,否则会被当做普通的字符串
var reg = /(\w{3}) is \6/;
reg.test( 'kid is kid' ); // false
reg.test( 'kid is \6' );  // true

2.String supports regular expressions

(1)search

Function: Find whether there is a matching regular string in the string, if there is, return the subscript of the position where the string appears for the first time, if not, return null.

Note: Whether or not there is a global match in the regex will not affect the returned result

var str = 'hello world hello';
var reg = /hello/;
var reg2 = /hello/g;
console.log(str.search(reg)); //返回 0
console.log(str.search(reg2));//返回 0

(2)match

Function: Match a string that matches the regular expression in the string, and return an array of the string, including the content and position of the string.

Note: If the regular setting is set to match globally, all string arrays that match the regular expression will be returned at one time

var str = 'hello world hello';
var reg1 = /hello/;
var reg2 = /hello/g;
console.log(str.match(reg1));
// 返回该字符串的一个数组,其中包括正则表达式、检测的字符串内容、第一个匹配的位置
// [ 'hello', index: 0, input: 'hello world hello', groups: undefined ]
console.log(str.match(reg2));
// 如果正则设置全局匹配,则一次性返回所有符合正则表达式的字符串数组
// [ 'hello', 'hello' ]

If a group is added , return the string that meets the requirements and an array of the group, but if global matching is enabled at the same time, the group content will not be added to the array.

var str = 'hello world hello';
var reg1 = /(he)llo/;
var reg2 = /(he)llo/g;
console.log(str.match(reg1));
// 只添加了分组,返回符合要求的字符串以及分组的一个数组
// [
//   'hello',
//   'he',
//   index: 0,
//   input: 'hello world hello',
//   groups: undefined
// ]
console.log(str.match(reg2));
// 添加分组同时开启全局匹配则 不会在数组中添加分组内容
// [ 'hello', 'hello' ]

(3)split

Role: split a string in a specific form

var str = "terry134briup156lisi12zhangsan";
var reg = /\d+/;// 当数字出现一次或多次时
var result = str.split(reg);// 以匹配正则表达式的字符串进行分割
console.log(result); // [ 'terry', 'briup', 'lisi', 'zhangsan' ]

(4)replace

Function: replace the content that satisfies the regular expression condition

var str = 'javascript'
// 如果开启全局模式 则替换所有满足条件的字符
var reg = /javascript/;
// replace(正则表达式, 要替换的内容)
var result = str.replace(reg, 'java');
console.log(result); //java
console.log(str); //javascript

3. Forward expressions

Expression: ?=exp(positive lookahead) ?!exp(negative lookahead)

Function: positive look forward, match the position that satisfies the expression exp. Negative lookahead, match the position that does not satisfy the expression exp.

Positive lookahead:

var str = 'Hello, Hi, I am Hilary.';
// 后面一定要匹配什么
var reg = /H(?=i)/g;// 这里表示必须满足H后面紧挨着i
var newStr = str.replace(reg, "T");// 只要满足上面的条件的H,将被替换成T
console.log(newStr);//Hello, Ti, I am Tilary.

Negative lookahead:

var str = 'Hello, Hi, I am Hilary.';
// 后面一定不要匹配什么
var reg = /H(?!i)/g; //H后面一定不能是i
var newStr = str.replace(reg, "T");// H后面不是紧挨着i的满足,满足的H将被替换为T
console.log(newStr);//Tello, Hi, I am Hilary.

Guess you like

Origin blog.csdn.net/qq_50748038/article/details/126733292