使用正则表达式的方法
方法 | 描述 |
---|---|
exec | 一个在字符串中执行查找匹配的RegExp方法,它返回一个数组(未匹配到则返回 null)。 |
test | 一个在字符串中测试是否匹配的RegExp方法,它返回 true 或 false。 |
match | 一个在字符串中执行查找匹配的String方法,它返回一个数组,在未匹配到时会返回 null。 |
matchAll | 一个在字符串中执行查找所有匹配的String方法,它返回一个迭代器(iterator)。 |
search | 一个在字符串中测试匹配的String方法,它返回匹配到的位置索引,或者在失败时返回-1。 |
replace | 一个在字符串中执行查找匹配的String方法,并且使用替换字符串替换掉匹配到的子字符串。 |
split | 一个使用正则表达式或者一个固定字符串分隔一个字符串,并将分隔后的子字符串存储到数组中的 String 方法。 |
1: exec 改变lastIndex
const regex = RegExp('foo*', 'g');
const str = 'table football, foosball';
let array1;
while ((array1 = regex1.exec(str1)) !== null) {
console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`);
// expected output: "Found foo. Next starts at 9."
// expected output: "Found foo. Next starts at 19."
}
第一次执行:
regex.exec(str) // ["foo", index: 6, input: "table football, foosball", groups: undefined]
第二次执行:
regex.exec(str) // ["foo", index: 16, input: "table football, foosball", groups: undefined]
2: test 改变lastIndex
const regex = RegExp('foo*', 'g');
const str = 'table football, foosball';
// 第一次执行
regex.lastIndex // 0
regex.test(str) // true
regex.lastIndex // 9
// 第二次执行
regex.test(str) // true
regex.lastIndex // 19
// 第三次执行
regex.test(str) // false
regex.lastIndex // 0
和lastIndex 有关
3: match
1:全匹配可以得到一个数组
const regex = RegExp('foo*', 'g');
const str = 'table football, foosball';
str.match(regex) // ["foo", "foo"]
2: 使用子表达式的开始和结束位置 可以得到 附加属性
var str = 'For more information, see Chapter 3.4.5.1';
var re = /see (chapter \d+(\.\d)*)/i;
var found = str.match(re);
console.log(found);
// logs [ 'see Chapter 3.4.5.1',
// 'Chapter 3.4.5.1',
// '.1',
// index: 22,
// input: 'For more information, see Chapter 3.4.5.1' ]
// 'see Chapter 3.4.5.1' 是整个匹配。
// 'Chapter 3.4.5.1' 被'(chapter \d+(\.\d)*)'捕获。
// '.1' 是被'(\.\d)'捕获的最后一个值。
// 'index' 属性(22) 是整个匹配从零开始的索引。
// 'input' 属性是被解析的原始字符串。
4: matchAll
回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器
const regex = RegExp('foo*', 'g');
const str = 'table football, foosball';
str.matchAll(regex) // RegExpStringIterator {}
[...str.matchAll(regex)] // [Array(1), Array(1)]
// 0: ["foo", index: 6, input: "table football, foosball", groups: undefined]
// 1: ["foo", index: 16, input: "table football, foosball", groups: undefined]
// length: 2
5: search
返回正则表达式在字符串中首次匹配项的索引 否则,返回 -1
。
const regex = RegExp('foo*', 'g');
const str = 'table football, foosball';
str.search(regex) // 6
str.indexOf(regex) // -1 indexOf不支持正则
6: replace
str.replace(regexp|substr, newSubStr|function)
指定一个函数作为参数
你可以指定一个函数作为第二个参数。在这种情况下,当匹配执行后,该函数就会执行。 函数的返回值作为替换字符串。 (注意:上面提到的特殊替换参数在这里不能被使用。) 另外要注意的是,如果第一个参数是正则表达式,并且其为全局匹配模式,那么这个方法将被多次调用,每次匹配都会被调用。
下面是该函数的参数:
变量名 | 代表的值 |
match | 匹配的子串。(对应于上述的$&。) |
p1,p2, ... | 假如replace()方法的第一个参数是一个 |
offset | 匹配到的子字符串在原字符串中的偏移量。(比如,如果原字符串是 |
string | 被匹配的原字符串。 |
NamedCaptureGroup | 命名捕获组匹配的对象 |
var str = 'x-x_';
var reg = /(x_*)|(-)/g
str.replace(reg , function(match, p1, p2) {
console.log(match, p1, p2)
});
// x x undefined
// - undefined -
// x_ x_ undefined
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
// Smith, John
console.log(newstr);
简易的mustache解释器可以用以下方式实现
const str = "{{aaaaa}} {{bbbbbbb}}"
const reg = /\{\{([\w\.]*)\}\}/g
const res = str.replace(reg ,function(str,key){
console.log(str,key)
return key;
});
console.log 输出
// {{aaaaa}} aaaaa
// {{bbbbbbb}} bbbbbbb
res // "aaaaa bbbbbbb"
6: split
var myString = "Hello 1 word. Sentence number 2.";
var splits = myString.split(/(\d)/);
console.log(splits); //[ "Hello ", "1", " word. Sentence number ", "2", "." ]