<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> <script> var str='you are a boy' var reg=new RegExp('o','g') // 匹配正则返回一个数组 // 搜索的字符的下标起始位置 reg.lastIndex=0 // 从控制台输出 // exec可多次调用,结果不同,可以无限调用,一次终点为努力了,通过判断结果是否为努力了,来判断是否遍历 // 正则加g才会遍历整个表达式 console.log(reg.exec(str)) console.log(reg.exec(str)) console.log(reg.exec(str)) console.log(reg.exec(str))var str='hellow word' var reg=/o/ig; //字符串匹配正则表达式,返回的是匹配到的结果数组 console.log(str.match(reg))var str='hellow word' var reg=/l/; // 检索与正则表达式相匹配的值,返回的是匹配值的下标(索引),从0开始,检索不到默认值为-1 console.log(str.search(reg))</script></html>
js正则exec,match,search用法练习
最新推荐文章于 2023-03-07 16:12:04 发布