天天看点

编译器的matchAll踩坑(√) & 正则总结(×)

起因是字符串匹配

给定一个id,和字符串s,找到和s[id]相等,并且距离id最近的下标

那么我们直接matchAll找,按说正常写法是这样的……(仅匹配)

非常坑…… 牛客、赛码平台上都是没有matchAll的。match有

String.prototype.matchAll() is part of the ECMAScript 2020 specification (draft). In TypeScript you can include these library features by adding <code>es2020</code> or <code>es2020.string</code>, in the compiler options: 

Checking the ts signature for the String and RegExp classes I realized there is no signature for matchAll. One manner to solve it could be:

(1) 原始写法

var a='baaa'.matchAll('a');

let all_array_2=[...a];

console.log(all_array_2[0].index);

其实也可可以归结为一句话

var a=[...'baaa'.matchAll('a')][0].index;

(2) 修改后

简单的matchAll后返回一个Object [RegExp String Iterator] {}  这个就解构,用数组就行

需要把matchAll部分修改成 str['matchAll'] (regExp) 实际上就是str.改成了str['']

var matches=[...'baaa'['matchAll']('a')];

console.log(matches);

(3)我们不是‘aaaa’ 这样的字符串,而是变量名怎么做呢

直接重新定义正则 

cmp=‘a’

var e=new RegExp(cmp,"g")

str.matchAll(e)

(4)那么想要快速找到下标,不用这个怎么做呢

match加了g 下标里也没有(实际上传‘a’的话是会隐式的调用正则去转换的)(match只匹配一次会给index,多次就不会了

indexof有限制问题只会匹配一次,但是可以for循环继续往下找。

var b = s.indexOf("."); //返回值为1,即第一个字符.的下标位置

var e = s.indexOf(".", b + 1); //返回值为11,即第二个字符.的下标位置

(5)重点来看exec:

如果没有设置全局项 <code>/g</code>,该方法将始终返回第一个匹配项:

当全局匹配时,该方法每次返回一个匹配项,直到没有匹配项时返回 <code>null</code>:

简单,直接多弄几次,看返回值就可以了!!!!!

(6)exec、test(测有无)、match(一or多但没信息)、search(只会一次)、replace(替换)

(7)总体来看,好像exec这种比较主流。。

var x = "a.xxx.com b.xxx.com c.xxx.com";

希望得到 ["a","b","c"] 这三个结果

1. 正则需要 添加 g

2.exec 循环,直到返回空

代码如下,会输出 a b c 

var re = /\s?(.*?).xxx.com/g;

while( tempR = re.exec(x)){

  console.log(tempR[1]);

}

参考文档:http://www.w3school.com.cn/jsref/jsref_exec_regexp.asp

exec的功能比 match 强大

提示:请注意,无论 RegExpObject 是否是全局模式,exec() 都会把完整的细节添加到它返回的数组中。这就是 exec() 与 String.match() 的不同之处,后者在全局模式下返回的信息要少得多。因此我们可以这么说,在循环中反复地调用 exec() 方法是唯一一种获得全局模式的完整模式匹配信息的方法(其实还有matchall但是这个是后来才出的)。

matchAll必须设置成g,返回值:一个迭代器,可以使用for…of…,数组新增的扩展符(…)或Array.from()实现功能