javascriptの中のRegexpは簡単に分かります.
7288 ワード
/***************** RegExp :*********************/
/*
* exec() : 。
@param : ,
@return : (array), null
@explain : Array , 2 :
index -
input -
, ( , )
*/
var text = 'gid and var and global';
var patterns = /gid( and var( and global)?)?/;
var matches = patterns.exec(text);
console.log(matches.index); //0 0 , matches index 0
console.log(matches.input); //"gid and var and global"
console.log(matches[0]); //"gid and var and global"
console.log(matches[1]); //" and var and global" ( and var( and global)?)?
console.log(matches[2]); //" and global" ( and global)?
console.log(matches[3]); //undefined , return undefined
//exec() , (g), 。
// , exec() ,
// (g) , exec()
//{IE lastIndex g , 。}
var txt = 'cat, bat, sat, fat';
var patterns1 = /.at/;
var matches1 = patterns1.exec(txt);
console.log(matches1.index); //0 0
console.log(matches1[0]); //cat
console.log(patterns1.lastIndex); //0 。
matches1 = patterns1.exec(txt);
console.log(matches1.index); //0
console.log(matches1[0]); //cat
console.log(patterns1.lastIndex); //0
var patterns2 = /.at/g;
var matches2 = patterns2.exec(txt);
console.log(matches2.index); //0
console.log(matches2[0]); //cat
console.log(patterns2.lastIndex); //3
matches2 = patterns2.exec(txt);
console.log(matches2.index); //5
console.log(matches2[0]); //bat
console.log(patterns2.lastIndex); //8
/*
* test()
* @param : {type:String}
* @return : {type:Boolean}
* @explains : return true, return false,
*/
var stxt = '000-12-2354';
var patterns3 = /\d{3}-\d{2}-\d{4}/;
if(patterns3.test(stxt)){
console.log("match sucess!")
}
/*
* toLocalString() toString()
* @return : , 。
*/
var patterns4 = new RegExp("\\[bc\\]at","gi");
var patterns5 = /\[bc\]at/gi;
console.log(patterns4.toLocaleString(),patterns4.toString()); // /\[bc\]at/gi /\[bc\]at/gi
console.log(patterns5.toLocaleString(),patterns5.toString()); // /\[bc\]at/gi /\[bc\]at/gi
/***************** RegExp ( ):*********************/
/*
*
* input $_
* lastMatch $&
* lastParen $+
* leftContext $` input lastMatch
* multiline $* , 。
* rightContext $' input lastMatch
*
* :opera input、lastMatch、lastParen multiline
* IE multiline
*/
var text = 'this has been a short summer';
var pattern = /(.)hort/g;
if(pattern.test(text)){
console.log(RegExp.input + ';','shortAttr:' + RegExp.$_ + ';');//this has been a short summer
console.log(RegExp.leftContext); //"this has been a" short
console.log(RegExp.rightContext); //" summer" short
console.log(RegExp.lastMatch); //"short"
console.log(RegExp.lastParen); //"s" (.)
console.log(RegExp.multiline); //false;
}
// , ECMAScript , 。
var text1 = 'this has been a short summer';
var pattern1 = /(.)hort/g;
if(pattern1.test(text1)){
console.log(RegExp["$_"]); //"this has been a short summer"
console.log(RegExp["$`"]); //"this has been a "
console.log(RegExp["$'"]); //" summer"
console.log(RegExp["$&"]); //short
console.log(RegExp["$+"]); //s (.)
console.log(RegExp["$*"]); //false
}
/*
* , 9 。
* RegExp.$1、RegExp.$2....RegExp.$9
* , ..... 。 exec(),test() , 。
* test() ,RegExp $1,$2 。
*/
var text2 = 'this has been a short communication'
var pattern2 = /(..)ic(...)o(.)/;
if(pattern2.test(text2)){
console.log(RegExp.$1); //un
console.log(RegExp.$2); //ati
console.log(RegExp.$3); //n
}
/******************************* **********************/
/*
* match()
@param : , RegExp 。
@return : , exec() 。
* search(regexp)
@param : match() 。
@return : regexp 。 ; -1;
@explain :
* replace(regexp/string,string/function)
@param : RegExp ( ),( )
@param : ( )
@return : , string/function regexp/string 。
@explain : , , (g)
@extra : , ( ),
split(string/regexp,Array.length)
@param : , , regexp
@param : , 。
@return : array 。
*/
var text = 'cat, bat, sat, fat';
var pattern = /.at/;
// pattern.exec(text)
var matches = text.match(pattern);
console.log(matches.index); //0
console.log(matches[0]); //cat
console.log(pattern.lastIndex); //0
var text1 = 'sst, bat, sat, fat';
var pos = text1.search(/at/);
var pos1 = text1.search(/at/);
console.log(pos,pos1); //6 6 bat
var result = text.replace('at', 'oncat');
console.log(result); //concat, bat, sat, fat
var result1 = text.replace(/at/g, 'fs');
console.log(result1); //cfs, bfs, sfs, ffs
// ; $1, , (.at)
var result2 = text.replace(/(.at)/g, 'fuck[$1]');
console.log(result2); //fuck[cat], fuck[bat], fuck[sat], fuck[fat]
var colorText = "red,blue,green,yellow";
var r1 = colorText.split(",");
var r2 = colorText.split("," ,2);
var r3 = colorText.split(/[^\,]+/);
console.log(r1); //["red", "blue", "green", "yellow"]
console.log(r2); //["red", "blue"]
console.log(r3); //["", ",", ",", ",", ""]