javascript RegExp 对象

语法

 

var patt=new RegExp(pattern,modifiers);

或者更简单的方式:

var patt=/pattern/modifiers;

 

  • pattern(模式) 描述了表达式的模式
  • modifiers(修饰符) 用于指定全局匹配、区分大小写的匹配和多行匹配

 

注意:当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 \)。比如,以下是等价的:

var re = new RegExp("\\w+");
var re = /\w+/;

RegExp 修饰符

修饰符用于执行不区分大小写和全文的搜索。

i - 修饰符是用来执行不区分大小写的匹配。(所有主要浏览器都支持i修饰符)

g - 修饰符是用于执行全文的搜索(而不是在找到第一个就停止查找,而是找到所有的匹配)。

m - 执行多行匹配

new RegExp("regexp","g")

或者更简单方式:

/regexp/g
对 "is" 进行全局且大小写不敏感的搜索:
var
str="Is this all there is?"; var patt1=/is/gi; document.write(str.match(patt1)); 结果 Is , is , is

test()

test()方法搜索字符串指定的值,根据结果并返回真或假。

语法:RegExpObject.test(string)

下面的示例是从字符串中搜索字符 "e" :

var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));

结果: true
var str="Hello world!";
//查找"Hello"
var patt=/Hello/g;
var result=patt.test(str);
document.write("返回值: " +  result); 
//查找 "W3Cschool"
patt=/W3Cschool/g;
result=patt.test(str);
document.write("<br>返回值: " +  result);
结果:
返回值: true
返回值: false

当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 \)

var str = 'runoob';
var patt1 = new RegExp('\\w', 'g'); // 有转义作为正则表达式处理
var patt2 = new RegExp('\w', 'g');  // 无转义作为字符串处理
var patt3 =/\w+/g;  // 与 patt1 效果相同
document.write(patt1.test(str)) //输出 true
document.write("<br>") 
document.write(patt2.test(str)) //输出 false
document.write("<br>") 
document.write(patt3.test(str)) //输出 true

exec()

exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

语法:RegExpObject.exec(string)

下面的示例是从字符串中搜索字符 "e" :

var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));

结果: e
var str="Hello world!";
//查找"Hello"
var patt=/Hello/g;
var result=patt.exec(str);
document.write("返回值: " +  result); 
//查找 "W3Cschool"
patt=/W3Cschool/g;
result=patt.exec(str);
document.write("<br>返回值: " +  result);
结果:
返回值: Hello
返回值: null

方括号

方括号用于查找某个范围内的字符:

[abc]语法:

new RegExp("[abc]")

或者更简单方式:

/[abc]/

所有主要浏览器都支持 [abc] 表达式

var str="Is this all there is?";
var patt1=/[a-h]/g;
document.write(str.match(patt1));

结果: h,a,h,e,e

[^abc]语法:

new RegExp("[^xyz]")

或者更简单方式:

/[^xyz]/

所有主要浏览器都支持 [^abc] 表达式

var str="Is this all there is?";
var patt1=/[^a-h]/g;
document.write(str.match(patt1));

结果: I,s, ,t,i,s, ,l,l, ,t,r, ,i,s,?

元字符

元字符(Metacharacter)是拥有特殊含义的字符:

.语法:

new RegExp("regexp.")

或者更简单方式:

/regexp./

var str="That's hot!";
var patt1=/h.t/g;
document.write(str.match(patt1));

结果: hat,hot

\w语法:

\w 元字符用于查找单词字符。
单词字符包括:a-z、A-Z、0-9,以及下划线, 包含 _ (下划线) 字符。
new RegExp("\w")
或者更简单方式:
/\w/
所有主要浏览器都支持 \w 元字符
var str="Give 100%!"; 
var patt1=/\w/g;
document.write(str.match(patt1));
结果: G,i,v,e,1,0,0

\W语法:

\W 元字符用于查找非单词字符。
单词字符包括:a-z、A-Z、0-9,以及下划线。
new RegExp("\W")
或者更简单方式:
/\W/
所有主要浏览器都支持 \W 元字符
var str="Give 100%!"; 
var patt1=/\W/g;
document.write(str.match(patt1));
结果: ,%,!

\d语法:

\d 元字符用于查找数字字符。
new RegExp("\d")
或者更简单方式:
/\d/
所有主要浏览器都支持 \d 元字符
var str="Give 100%!"; 
var patt1=/\d/g;
document.write(str.match(patt1));
结果: 1,0,0

\D语法:

\D 元字符用于查找非数字字符。
new RegExp("\D")
或者更简单方式:
/\D/
所有主要浏览器都支持 \D 元字符
var str="Give 100%!"; 
var patt1=/\D/g;
document.write(str.match(patt1));
结果: G,i,v,e, ,%,!

\s语法:

\s 元字符用于查找空白字符。
空白字符可以是:
空格符 (space character)
制表符 (tab character)
回车符 (carriage return character)
换行符 (new line character)
垂直换行符 (vertical tab character)
换页符 (form feed character)
new RegExp("\s")
或者更简单方式:
/\s/
所有主要浏览器都支持 \s 元字符
var str="Is this all there is?"; 
var patt1=/\s/g;
document.write(str.match(patt1));
结果: , , ,

\S语法:

\S 元字符用于查找非空白字符。
空白字符可以是:
空格符 (space character)
制表符 (tab character)
回车符 (carriage return character)
换行符 (new line character)
垂直换行符 (vertical tab character)
换页符 (form feed character)
new RegExp("\S")
或者更简单方式:
/\S/
所有主要浏览器都支持 \S 元字符
var str="Is this all there is?"; 
var patt1=/\S/g;
document.write(str.match(patt1));
结果: I,s,t,h,i,s,a,l,l,t,h,e,r,e,i,s,?

\b语法:

\b 元字符匹配单词边界。
\b 元字符通常用于查找位于单词的开头或结尾的匹配。
new RegExp("\bregexp")
或者更简单方式:
/\bregexp/
所有主要浏览器都支持 \b 元字符
var str="Visit W3CSchool"; 
var patt1=/\bW3/g;
document.write(str.match(patt1));
结果: W3

\B语法:

\B 元字符匹配非单词边界。匹配位置的上一个和下一个字符的类型是相同的:即必须同时是单词,或必须同时是非单词字符。字符串的开头和结尾处被视为非单词字符。
如果未找到匹配,则返回 nullnew RegExp(" \Bregexp")
或者更简单方式:
/\Bregexp/
var str="Visit W3Cschool"; 
var patt1=/\Bschool/g;
document.write(str.match(patt1));
结果: school

\n语法:

\n 元字符用于查找换行符。
\n 返回换行符被找到的位置。如果未找到匹配,则返回 -1new RegExp("\n")
或者更简单方式:
/\n/
var str="Visit W3Cschool.\n Learn JavaScript."; 
var patt1=/\n/g;
document.write(str.search(patt1));
结果: 16

\xxx语法:

\xxx 元字符用于查找以八进制数 xxx 规定的字符。
如果未找到匹配,则返回 nullnew RegExp("\xxx")
或者更简单方式:
/\xxx/
var str="Visit W3Cschool. Hello World!"; 
var patt1=/\127/g;
document.write(str.match(patt1));
结果:  W,W

\xdd语法:

\xdd 元字符查找以十六进制数 dd 规定的字符。
如果未找到匹配,则返回 nullnew RegExp("\xdd")

或者更简单方式:

/\xdd/
var str="Visit W3Cschool. Hello World!"; 
var patt1=/\x57/g;
document.write(str.match(patt1));
结果: W,W

\uxxxx语法:

\uxxxx 元字符用于查找以十六进制数 xxxx 规定的 Unicode 字符。
如果未找到匹配,则返回 nullnew RegExp("\uxxxx")

或者更简单方式:

/\uxxxx/
var str="Visit W3Schools. Hello World!"; 
var patt1=/\u0057/g;
document.write(str.match(patt1));
结果: W,W

量词

RegExp 对象方法

compile()语法:

compile() 方法用于在脚本执行过程中编译正则表达式。

compile() 方法也可用于改变和重新编译正则表达式。

除了 Opera 浏览器外,其他浏览器都支持 compile() 方法。

var str="Every man in the world! Every woman on earth!";
var patt=/man/g;
var str2=str.replace(patt,"person");
document.write(str2+"<br>");
patt=/(wo)?man/g;
patt.compile(patt); 
str2=str.replace(patt,"person");
document.write(str2);
结果: 
Every person in the world! Every woperson on earth!
Every person in the world! Every person on earth!

支持正则表达式的 String 对象的方法

search 语法:

search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。

如果没有找到任何匹配的子串,则返回 -1。

string.search(searchvalue)

实例:执行一次对大小写敏感的查找:

function myFunction(){
    var str="Mr. Blue has a blue house"
    var n=str.search("blue");
    document.getElementById("demo").innerHTML=n;
}
结果: 15

执行一次忽略大小写的检索:

function myFunction(){
    var str="Mr. Blue has a blue house"
    var n=str.search(/blue/i);
    document.getElementById("demo").innerHTML=n;
}
结果: 4

match 语法:

match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

注意: match() 方法将检索字符串 String Object,以找到一个或多个与 regexp 匹配的文本。这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g。如果 regexp 没有标志 g,那么 match() 方法就只能在 stringObject 中执行一次匹配。如果没有找到任何匹配的文本, match() 将返回 null。否则,它将返回一个数组,其中存放了与它找到的匹配文本有关的信息。

string.match(regexp)

全局查找字符串 "ain",且不区分大小写:

function myFunction(){
    var str = "The rain in SPAIN stays mainly in the plain"; 
    var n=str.match(/ain/gi);
    document.getElementById("demo").innerHTML=n;
}
结果: ain,AIN,ain,ain

replace()语法:

replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串

该方法不会改变原始字符串。

string.replace(searchvalue,newvalue)

执行一个全局替换:

<p>单击按钮将段落中的“blue”替换成“red”。</p>
<p id="demo">Mr Blue has a blue house and a blue car.</p>
<button onclick="myFunction()">点我</button>
function myFunction(){
    var str=document.getElementById("demo").innerHTML; 
    var n=str.replace(/blue/g,"red");
    document.getElementById("demo").innerHTML=n;
}

结果: 
单击按钮将段落中的“blue”替换成“red”。
Mr Blue has a red house and a red car.

执行一个全局替换, 忽略大小写:

<p>单击按钮将段落中的“blue”替换成“red”。</p>
<p id="demo">Mr Blue has a blue house and a blue car.</p>
<button onclick="myFunction()">点我</button>

function myFunction(){
    var str=document.getElementById("demo").innerHTML; 
    var n=str.replace(/blue/gi,"red");
    document.getElementById("demo").innerHTML=n;
}

结果:
单击按钮将段落中的“blue”替换成“red”。
Mr red has a red house and a red car.

split()语法:

split() 方法用于把一个字符串分割成字符串数组。

提示: 如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割。

注意: split() 方法不改变原始字符串。

string.split(separator,limit)

省略分割参数:

<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
    var str="How are you doing today?";
    var n=str.split();
    document.getElementById("demo").innerHTML=n;
}
</script>
结果: How are you doing today?

分割每个字符,包括空格:

<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
    var str="How are you doing today?";
    var n=str.split("");
    document.getElementById("demo").innerHTML=n;
}
</script>
结果: H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?

使用 limit 参数:

<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
    var str="How are you doing today?";
    var n=str.split(" ",3);
    document.getElementById("demo").innerHTML=n;
}
</script>
结果: How,are,you

使用一个字符作为分隔符:

<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
    var str="How are you doing today?";
    var n=str.split("o");
    document.getElementById("demo").innerHTML=n;
}
</script>
结果: H,w are y,u d,ing t,day?

 

posted @ 2017-04-13 17:41  JoyJin  阅读(1434)  评论(0编辑  收藏  举报