正则表达式中match和exec, test
// test // 判断字符串是否满足某个匹配模式,满足的话,返回true,否则,返回false let reg = /^a/ let str = ‘123abc‘ console.log(reg.test(str)) //false // exec // 用于检索字符串中的正则表达式的匹配。该函数返回一个数组,其中存放匹配的结果 // 如果未找到匹配,则返回值为null let reg1 = /^a/ let str1 = ‘abc123abc‘ console.log(reg1.exec(str1)) //[ ‘a‘, index: 0, input: ‘abc123abc‘, groups: undefined ] // match // 也是用于匹配,分全局匹配和非全局匹配 // 非全局 let str3 = ‘Today is the 186th day of 2018,I must finish these 2 projects within 21 days.‘ let result1 = str3.match(/\d+/) console.log(result1) // 结果 // [ // ‘186‘, // index: 13, // input: ‘Today is the 186th day of 2018,I must finish these 2 projects within 21 days.‘, // groups: undefined // ] // 全局 let str4 = ‘Today is the 186th day of 2018,I must finish these 2 projects within 21 days.‘ let result2 = str3.match(/\d+/g) console.log(result2) // 结果 [ ‘186‘, ‘2018‘, ‘2‘, ‘21‘ ]
相关推荐
敏敏张 2020-11-11
SCNUHB 2020-11-10
小木兮子 2020-11-11
wwwsurfphpseocom 2020-10-28
WasteLand 2020-10-18
Cocolada 2020-11-12
杜鲁门 2020-11-05
shirleypaddy 2020-10-19
qingmumu 2020-10-19
Testingba工作室 2020-09-15
周公周金桥 2020-09-13
专注前端开发 2020-08-16
emagtestage 2020-08-16
heniancheng 2020-08-15
hanjinixng00 2020-08-12
小方哥哥 2020-08-09
83327712 2020-07-30
卖小孩的咖啡 2020-07-21
wqiaofujiang 2020-07-05