1.科普:RegExp是js原生的内置对象,直接可以使用。e.g. :via RegExp.$1 取得第一个匹配子项;
默认带特殊含义的字符若要表达其本身就在其前面加个\转义,这些字符共有11个:{} [] () * + . ?\反斜杠转义 或| 头^ 尾$ ,(如果记不住,伸出你的双手,每个手指绑定一个符号就轻松搞定10个了呢,如果还是记不住,那就罚抄10遍吧,哈哈哈)
2.实例:正则对象的exec() 执行后返回一个匹配结果的数组 或者null,例如:
var matches = /(#)(.*)/g.exec("#id") ; // 返回一个数组
详解:(#)== RegExp.$1 , (.*) == RegExp.$2
RegExp.$0是什么呢?RegExp.$0 == "#id" 就是被exec的原始字符串
3.简写/indus(try|tries)/ 等价于/indus(?:try|tries)/,可取得(?:try|tries)的匹配值
/indus(?:try|tries)/g.exec("industries") // industries
/indus(try|tries)/g.exec("industries") // industries
匹配结果如下图:
4.不会获取(?=home)的匹配,它只是一种匹配模式(正向肯定预查,正向否定预查,反向肯定预查,反向否定预查)
4.1:正向肯定预查,执行结果不包含正则中()的内容,例如:
var matches = /go(?=home)/g.exec("gohome gohomee nogohome") // matches [0] = go
正向肯定预查被广泛用于String对象replace()替换某些字符,例如:
var str = "goxhome gohome"
var result = str.replace(/(go)(?=home)/g,"'$1out'") // result = "goxhome 'goout'home"
4.2:正向否定预查,执行结果不包含正则中(?!home)
的内容,例如:全局g将多次执行exec
let result;
let strObj = "gohome goxhome goyhome";
let regexp = /go(?!home)/g;
result = regexp.exec(strObj);
console.log("result=",result[0]); // go
console.log("lastIndex=",regexp.lastIndex); // 9
result = regexp.exec(strObj);
console.log("result=",result[1]); // go
console.log("lastIndex=",regexp.lastIndex); // 17
4.3:反向肯定预查,执行结果不包含正则中(?<=on)
的内容,例如: 全局g将多次执行exec
let result;
let strObj = "undertable ontable besidetable";
let regexp = /(?<=on)table/g;
result = regexp.exec(strObj);
console.log("result=",result);// table
console.log("lastIndex=",regexp.lastIndex); // 18
result = regexp.exec(strObj);
console.log("result=",result); // null
console.log("lastIndex=",regexp.lastIndex);// 0
4.4:反向否定预查,执行结果不包含正则中(?<!on)
的内容,例如: 全局多次执行exec
let result;
let strObj = "undertable ontable besidetable";
let regexp = /(?<!on)table/g;
result = regexp.exec(strObj);
console.log("result=",result); // table
console.log("lastIndex=",regexp.lastIndex); // 10
result = regexp.exec(strObj);
console.log("result=",result); // table
console.log("lastIndex=",regexp.lastIndex);// 30
new RegExp的要点:
new RegExp("\\w",“gi”) 等价于 /\w/gi
new RegExp(“\\w”,“gim”)如果是g(全局匹配,如果没g,即使循环.exec一万次,仍只重复得到‘匹配到的第一个结果’),则要循环才能匹配出所有结果。因其本身即使有多个结果,但它一旦找到结果就会停下,所以要循环执行某个正则对象的exec去找。
lastIndex是最近一次匹配结果的index+1位。
全局exec示例:
var str = "Visit W3School, W3School is a place to study web technology.";
var patt = new RegExp("W3School","g"); // 等价 var patt = /W3School/g
var result;
while (( result = patt.exec(str)) != null ) { // 这里绝不能写成 /W3School/g.exec(str),因为这相当于又new了对象,不是原来的那个RegExp了,所以就会死循环。一定要是原来的patt才行
document.write(result);
document.write("<br />");
document.write(patt.lastIndex);
document.write("<br />");
}
执行结果:
W3School
14
W3School
24
new RegExp("\\w","gim") === /\w/gim
Brief summary:
1.注意/regexp/g.exec(); 全局匹配g一定不可忘记写,且需要多次执行,因为exec执行一次只能得到一次的结果。
2.strObject.replace(/regexp/g,"xx"),在执行全局匹配时,不必像exec一样执行多次,但一定要加g,否则只能替换第一次匹配,如下示例:
let strObj = "gohome goxhome goyhome"
strObject.replace(/go(?!home)/g,"leave-"); // "gohome leave-xhome leave-yhome"
如果没有全局匹配g,如下:
strObject.replace(/go(?!home)/,"leave-"); // "gohome leave-xhome goyhome",只更改了第一个
更多基本知识 see also: http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp
更详细实用的基础示例,包括分组匹配等:https://www.cnblogs.com/mq0036/p/6013225.html
特别喜欢魔道祖师主角魏婴的顽皮机敏、正直坚毅、义薄云天。
非常期待收获这样的友情 ^_^^_^^_^