[JavaScript]正则表达式学习

 

$字符串的结尾:

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=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值