正则表达式总结

使用正则表达式的方法

方法描述
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()方法的第一个参数是一个RegExp 对象,则代表第n个括号匹配的字符串。(对应于上述的$1,$2等。)例如,如果是用 /(\a+)(\b+)/ 这个来匹配,p1 就是匹配的 \a+p2 就是匹配的 \b+

offset

匹配到的子字符串在原字符串中的偏移量。(比如,如果原字符串是 'abcd',匹配到的子字符串是 'bc',那么这个参数将会是 1)

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", "." ]

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值