欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[JavaScript]正则表达式学习 博客分类: Web前端 正则表达式JavaScriptVBVBScript 

程序员文章站 2024-03-18 21:28:52
...

 

$字符串的结尾:

document.write("bitan".search(/n$/) + "," + "bitan".search(/it/));

 


 

4,1

|选择:

var a = "2005-9-13 12:05:36".split(/(-|\s|:)+/);
for (i in a)
{
document.write("[" + i + "=" + a[i] + "];");
}

 


 

[0=2005];[1=9];[2=13];[3=12];[4=05];[5=36];

String.match()与子表达式引用:

 

var a = "2005-9-13 12:05:36".match(/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/g );
for (i in a)
{
document.write("[" + i + "=" + a[i] + "];<br>");
}
document.write("-------------------------<br>");
var a = "2005-9-13 12:05:36".match(/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/);
for (i in a)
{
document.write("[" + i + "=" + a[i] + "];<br>");
}

document.write("-------------------------<br>");
var a = "23.0234182345305".match(/(\d+)\.(\d{1,4})/);
for (i in a)
{
document.write("[" + i + "=" + a[i] + "];<br>");
}

 


 

[input=2005-9-13 12:05:36];
[0=2005-9-13 12:05:36];
[index=0];
[lastIndex=18];
-------------------------
[input=2005-9-13 12:05:36];
[index=0];
[lastIndex=18];
[0=2005-9-13 12:05:36];
[1=2005];
[2=9];
[3=13];
[4=12];
[5=05];
[6=36];
-------------------------
[input=23.0234182345305];
[index=0];
[lastIndex=7];
[0=23.0234];
[1=23];
[2=0234];

String.replace()与子表达式引用:

 

var a = "2005-04-15 12:05:32".replace(/\d+(-|\s|:) \d+\1 \d+(-|\s|:) \d+(-|\s|:) \d+\3 \d+/, "####$1 ##$1 ##$2 ##$3 ##$3 ##");
document.write(a);

 


 

####-##-## ##:##:##

\b词语边界:

 

document.write("Java is not JavaScript".replace(/\b Java\b /gi,"VB") + "<br>");
document.write("Java is not JavaScript".replace(/Java/gi,"VB"));



 

<!--StartFragment -->VB is not JavaScript
VB is not VBScript

RegExp.multiline属性:

 

var s = "I come.\n I see.\n I conquer.\n ";
document.write(s.search(/see\.$ /) + "<br>");
RegExp.multiline = true;
document.write(s.search(/see\.$ /) + "<br>");



 

<!--StartFragment -->-1
10

RegExp的类属性列表:

 

for (i in RegExp)
{
document.write(i + "=" + RegExp[i] + "<br>");
}

 


 

<!--StartFragment -->input=
multiline=false
lastMatch=
lastParen=
leftContext=
rightContext=
$1=
$2=
$3=
$4=
$5=
$6=
$7=
$8=
$9=