/***************** RegExp對象的主要方法:*********************/
/*
* exec() : 該方法專門為擷取的群組設計的。
@param : 接受一個參數,要應用模式的字串
@return : 返回包含第一個匹配項資訊的數組(array),沒有匹配項的情況下返回null
@explain : 返回的數組是Array的執行個體,並且包含2個額外的屬性:
index - 表示匹配項在字串中的位置
input - 表示應用Regex的字串
數組中的第一項是與整個模式比對的字串,其他項是模式中的擷取的群組匹配的字串(如果沒有擷取的群組,則返回數組只包含一項)
*/
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" 表示Regex要匹配的字串
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 :返回Regex的字面量形式,與建立Regex的方式無關。
*/
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之後的文本
* 這些屬性適用於範圍中的所有Regex www.2cto.com
* 注意: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 : 一個參數,Regex或者RegExp對象。
@return : 返回一個數組,和exec()類似。
* search(regexp)
@param :和match()參數相同。
@return :字串中第一個與 regexp 相匹配的子串的起始位置。返回第一個匹配的索引;如果沒找到返回-1;
@explain :始終從字串的開頭向後尋找
* replace(regexp/string,string/function)
@param : 第一個參數是RegExp對象或者一個字串(字串不會被轉換為Regex),(用什麼模式或者字串來替換)
@param :第二個參數可以是個字串或者是一個函數(被替換的內容)
@return : 一個新的字串,是用 string/function 替換了 regexp/string 的第一次匹配或所有匹配之後得到的。
@explain :要替換所有子字串,必須提供一個Regex,並且設定了全域(g)
@extra : 如果第二個參數是個字串,可是可以使用字元序列(短屬性名稱方式),將Regex操作得到的值插入到結果字串中
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); //["", ",", ",", ",", ""]
摘自 Jalen