match、matchAll:从字符串中获取给定正则匹配到的内容。 注意全局匹配修饰符g对于结果的影响,其中matchAll返回的是一个结果迭代器。 replace、replaceAll:替换字符串中匹配到的内容为指定字符串。其中有两点需要注意: ① 第一个参数为正则 其中$&代表匹配到的字符串,在这里代表"-c",$1代表第一个捕获组,
// regex only letters not spacesconstreg =/^[A-Za-z]+$/; demo /** *@param{string}s*@return{string} */varreplaceSpace =function(s) {// return s.replaceAll(/ /g, '%20')// return s.replaceAll(/\s/g, '%20')// return s.split(' ').join('%20');letr ='';for(letcofs)...
console.log(str2.replace(/^Reg/, 'reg')) // hello world\nRegExp console.log(str2.replace(/^Reg/m, 'reg')) // hello world\nregExp const str3 = 'hello world hello RegExp' console.log(str3.replace(/ello/, 'ey')) // hey world hello RegExp console.log(str3.replace(/ello/g...
b=2,c=3';console.log([...str.matchAll(reg)]); // Uncaught TypeError: String.prototype.matchAll called with a non-global RegExp argument在可以使用matchAll的情况下,使用matchAll比使用exec方法更便捷一些。
new RegExp("pattern"[,"flags"])(即:new RegExp("模式"[,"标记"])) 参数: pattern(模式):表示正则表达式的文本 flags(标记):如果指定此项,flags可以是下面之一: g:global match(全定匹配) i:ignore case(忽略大小写) gi:both global match and ignore case(匹配所有可能的值,也忽略大小写) ...
console.log(yuan_str_1.match(yuan_reg_10)) let text_b_0 = "HELLO, LOOK AT YOU" // let pattern_b_0 = /\bLO/ // let pattern_b_01 = /LO\b/ // const pt = text_b_0.match(pattern_b_0) // console.log(pt, pt.length, pt[0], pt.join()) // [ 'LO', index: 7, in...
exec():一个用来搜索一个匹配的regExp方法,用于在字符串中查找指定正则表达式,如果 exec()方法执行成 功,则返回包含该查找字符串的相关信息数组,如果执行失败,则返回null match(pattern):一个用来匹配一个字符串的string方法,返回pattern中的子串或者null replace(patternReg,replaceReg):一个用来完成替换操作的String...
str.matchAll(regexp) matchAll()方法返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器,如果传入一个非正则表达式对象,则会隐式地使用new RegExp(obj)将其转换为一个RegExp,传入的RegExp必须是设置了全局模式g的形式,否则会抛出异常TypeError,返回一个迭代器,不可重用,结果耗尽需要再次调用方法,获取一个...
log(hd.match(reg)); 零宽负向先行断言 (?!exp) 正向否定查找 用来检查接下来不应该出现匹配内容的字符集。 使用(?!exp)字母后面不能为两位数字 let reg = /abc(?!de)/; reg.test('abcdefg'); // false; reg.test('abcd'); // true; reg.test('abcabc'); // true; 零宽负向后行...
String.prototype.matchAll() String.prototype.replace() String.prototype.replaceAll() String.prototype.search() String.prototype.split() RegExp.prototype 首先我们要讲解的是RegExp对象上的两个方法 RegExp.prototype.test() 作用:检测给定的字符串中是否有满足正则的匹配 ...