EegExp 范例
ECMAScript 经由过程RegExp 范例来支撑正则表达式。语法以下:
var expression = / pattern / flags;
每一个正则表达式都可带有一或多个标志(flags),正则表达式的婚配情势支撑以下3 个标志。
g:示意全局(global)情势,该情势将被应用于一切字符串,而非在发明第一个婚配项时马上住手;
i:示意不辨别大小写(case-insensitive)情势,该情势在肯定婚配项时疏忽情势与字符串的大小写;
m:示意多行(multiline)情势,在抵达一行文本末端时还会继承查找下一行中是不是存在与情势婚配的项;
如:
var pattern1 = /at/g; //婚配字符串中一切“at”的实例
var pattern2 = /[bc]at/i; //婚配字符串第一个“bat”或“cat”,不辨别大小写
var pattern3 = /.at/gi; //婚配字符串中一切以“at”末端的3个字符串的组合,不辨别大小写与其他语言中的正则表达式相似,情势中运用的一切元字符都必需转义。正则表达式中的元字符包括:
( [ { \ ^ $ | ? * + . ] )
如:
var pattern1 = /\[bc\]at/i; //婚配第一个“[bc]at”,不辨别大小写;
var pattern2 = /\.at/gi; //婚配一切“.at”,不辨别大小写;
别的,还可以运用RegExp 组织函数,它吸收两个参数:一个是要婚配的字符串情势,另一个是可选的标志字符串。如:
var pattern1 = new RegExp("[bc]at","i");
由于RegExp 组织函数的情势参数是字符串,所以在某些情况下要对字符串举行两重转义。一切元字符串都必需两重转义,如\n 通常被转义为\\n,而在正则表达式中就会变成\\\n。如:
/\[bc\]at/ => \\[bc\\]at
/\.at/ => \\.at
/name\/age/ => name\\/age
/\d.\d{1,2}/ => \\d.\\d{1,2}
/\w\\hello\\123/ => \\w\\\\hello\\\\123
正则表达式字面两始终会同享同一个RegExp 实例,而运用组织函数建立的每一个新RegExp 实例都是一个新实例。如:
var re = null,i;
for (var i = 0; i < 10; i ++){
re = /cat/g;
re.test("catastrophe");
}
for (var i = 0; i < 10; i ++){
re = new RegExp("cat","g");
re.test("catastrophe");
}
关于第一个,由于会测试到字符串末端,所以下次再挪用test()
就要从头最先。而第二个轮回运用RegExp 组织函数在每次轮回冲建立正则表达式。由于媒体迭代都邑建立一个新的RegExp 实例,所以每次挪用text()
都邑返回true。
RegExp 实例属性
global:布尔值,是不是设置了g;
ignoreCase:布尔值,是不是设置了i;
multiline:布尔值,是不是设置了m;
lastIndex:整数,最先搜刮下一个婚配项的字符位置,从0 最先算起;
source:正则表达式的字符串示意,根据字面量情势返回;
如:
var pattern = new RegExp("\\[bc\\]at","i");
document.write(pattern.global); //false
document.write(pattern.ignoreCase); //true
document.write(pattern.multiline); //false
document.write(pattern.lastIndex); //0
document.write(pattern.source); //\[bc\]at
注重末了一个,source 属性保留的是范例情势的字符串,就是字面量情势所用的字符串。
RegExp 实例要领
主要有两个要领,一个是exec()
要领,一个是test()
要领。
exec()
要领是特地为捕捉组而设想的。吸收一个字符串参数,然后返回包括第一个婚配项信息的数组;或许null;返回的数组还分外包括两个属性:index 和input。在数组中,第一项是与全部情势婚配的字符串,其他项是与情势中的不活组婚配的字符串。如:
var text = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/gi;
var matches = pattern.exec(text);
console.log(matches.index);
console.log(matches.input);
console.log(matches[0]);
console.log(matches[1]);
console.log(matches[2]);
/*
[Log] 0 (repetition.html, line 33)
[Log] mom and dad and baby (repetition.html, line 34)
[Log] mom and dad and baby (repetition.html, line 35)
[Log] and dad and baby (repetition.html, line 36)
[Log] and baby (repetition.html, line 37)
*/
由于全部字符串自身与情势婚配,所以返回的数组matches 的index 为0;数组中的第一项是婚配的全部字符串,第二项包括与第一个不活租婚配的内容,第三项包括与第二个捕捉组婚配的内容。
第一个例子,这是一个全局情势:
var text = "this is a Global setting not a global function";
var pattern = /global/gi;
var matches = pattern.exec(text);
console.log(matches); //["Global"]
console.log(matches.index); //10
console.log(matches.input); //this is a Global setting not a global function
console.log(matches[1]); //undefined 这里没有捕捉组
console.log(matches[0]); //Global
console.log(pattern.lastIndex); //16
matches = pattern.exec(text);
console.log(matches); //["global"]再次挪用该exec()则继承查找新的婚配项
console.log(matches.index); //31
console.log(pattern.lastIndex); //37
第二个例子,这不是一个全局情势:
var text = "this is a Global setting not a global function";
var pattern = /global/i;
var matches = pattern.exec(text);
console.log(matches); //["Global"]
console.log(matches.index); //10
console.log(pattern.lastIndex); //0
matches = pattern.exec(text);
console.log(matches); //["Global"] 这里仍然是Global,申明非全局情势会从头最先搜刮。
console.log(matches.index); //10
console.log(pattern.lastIndex); //0
全局情势,每次挪用exec()都邑返回字符串中的下一个婚配项;而非全局情势,每次挪用exec()返回的都是第一个婚配项。
test()
要领则是吸收一个字符串参数。在情势与该参数婚配的情况下返回true;不然返回false。通常在只想晓得目的字符串与某个情势是不是婚配,但不须要晓得文本内容的情况下,运用这个要领异常轻易。
var text = "testing!";
var pattern = /est/gi;
if (pattern.test(text)){
document.write("matched")
}else{
document.write("not matched")
}
RegExp 组织函数属性
这些属性有一个长属性名也有一个短属性名。最经常使用的有两个:
leftContext($` input 字符串中lastMatch 之前的文本);
rightContext ($’ input 字符串中lastMatch 以后的文本);
其他几个属性Opera 和IE 对此兼容不好。有:
input ($_ 近来一次要婚配的字符串);
lastMatch ($& 近来的一次婚配项);
lastParen ($+ 近来一次的捕捉组);
multiline ($* 返回布尔值,示意是不是一切表达式都运用多行情势);
如:
var text = "hello there";
var pattern = / /gi;
if(pattern.exec(text)){
document.write("targeted" + "<br/>");
document.write(RegExp.leftContext);
document.write(RegExp.rightContext);
}else{
document.write("missed" + "<br/>");
}
又如:
var text = "hello there";
var pattern = / /gi;
if(pattern.exec(text)){
document.write("targeted" + "<br/>");
document.write(RegExp.input); //hello there
document.write(RegExp.multiline); //false
}else{
document.write("missed" + "<br/>");
}
由于短属性名不是有用的标识符,因而必需经由过程方括号语法来接见它们。如RegExp["$'"]
别的,另有9 个用于存储捕捉组的组织函数属性。语法是RegExp.$1
`RegExp.$2`等等。如:
var text = "this has been a short summer";
var pattern = /(..)or(.)/g;
if (pattern.test(text)){
document.write(RegExp.$1);
document.write(RegExp.$2);
}
情势的缺点
详细接见情势的范围