javascript正規表現(7)(小事例)
1397 ワード
var names=
[" "," "," "," "];
var reg=new RegExp(
names.join("|"),"g");
var str=
" , ."+
" , "+
" ?"+
" ?"+
" , ?"+
" ";
// arr, null
var arr=null;
// : str reg , arr , null
while((arr=reg.exec(str))!=null){
// : ?, ?
// ?
console.log(
" "+arr.index+" "+arr[0]+", "+reg.lastIndex+" "
);
}
// html url
var html='<link rel="stylesheet" href="index.css"/><body><a name="top"></a><h1>Hello</h1><a class="curr" href="http://www.baidu.com">go to tmooc</a><a name="center"></a><a href="http://www.hao123.com" target="_blank">go to tedu</a></body>';
var reg=
/<a\s+[^>]*?href="([^"]*?)"[^>]*?>/g;
// arr, null
var arr=null;
// : html reg , arr , null
while((arr=reg.exec(html))!=null){
// url
console.log(arr[1]);
//RegExp.$1: 1
}
var reg=
/^(?![a-zA-Z]+$)(?![a-z0-9]+$)[a-zA-Z0-9]{6,8}$/;
// : , reg ,
while(
!reg.test(prompt(" "))
){
// :
alert(" ");
}//( )
// :
alert(" ");