js方法的使用(z)

http://www.108js.com/article/article1/10025.html?id=58javascript

 

 

javascript中正則匹配有3個方法,match,exec,test。這些方法都跟字符串和RegExp對象有關,但使用場景不同,容易混淆。match是字符串的一個方法,接收一個RegExp對象作爲參數,其餘的是RegExp對象的方法,接收一個字符串參數。

<script>
  var str = 'abcdef12ab34cd56ef';
  var patt = new RegExp('ab'); //注意是非全局匹配
  var ret_test = patt.test(str);
  alert(ret_test);

  var ret_match = str.match(patt);
  alert(ret_match);

  var ret_exec = patt.exec(str);
  alert(ret_exec);
</script>

點擊觀看效果

    

1. regExp.test(string)

  該方法最簡單,在string中找到匹配regExp的字符串則返回true,沒找到匹配的字符串則返回false

2. regExp.exec(string)

  該方法稍微複雜些。

  當regExp沒有全局標誌時,其返回值爲字符串數組:數組的第0號元素爲剛匹配到的字符串,若是regExp有子表達式,則數組第1號元素爲regExp的第一個子表達式,第2號元素爲regExp的第二個字表達式...以此類推。在上例中若是
patt = new RegExp('f(\\d)(\\d)','g');則 ret_exec 將爲字符串數組:['f12','1','2']。

  在設置g屬性後,雖然匹配結果不受g的影響,返回結果仍然是一個數組(第一個值是第一個匹配到的字符串,之後的爲分組匹配內容),可是會改變index和lastIndex等的值,將該對象的匹配的開始位置設置到緊接這匹配子串的字符位置,當第二次調用exec時,將從lastIndex所指示的字符位置開始檢索。一樣match方法在設置了g屬性後,也會改變index和lastIndex的值,可是是一次性的。沒法像exec那樣能逐過程累積,所以沒法累積獲取下一次檢索的位置。
<script>
  var patt = new RegExp('ab', 'g');
  var str = 'abcdef12ab34cd56ef';
  var ret;
  while((ret = patt.exec(str))!=null) {
    document.write(ret+"</br>");
    document.write("ret.input="+ret.input+"</br>");
    document.write("ret.index="+ret.index+"</br>");
    document.write("RegExp.lastIndex ="+RegExp.lastIndex +"</br>");

  }
</script>

運行:
ab
ret.input=abcdef12ab34cd56ef
ret.index=0
RegExp.lastIndex =2
ab
ret.input=abcdef12ab34cd56ef
ret.index=8
RegExp.lastIndex =10


exec方法返回的不是標準的數組,應該算是一個類數組,由於它還有2個屬性:input是輸入的字符串,index是當前匹配的字符串第一個字符在input中的位置。

3. string.match(regExp)

  該方法比exec簡單一些,由於它不用考慮regExp的lastIndex屬性。一樣,也須要分兩種狀況(全局匹配與非全局匹配)

  當regExp沒有全局標誌時,返回值與調用exec同樣,返回一個數組,數組的第0號元素爲剛匹配到的字符串,若是regExp有子表達式,則數組第1號元素爲regExp的第一個子表達式,第2號元素爲regExp的第二個字表達式...以此類推。主意該數組同時還有2個屬性:input是輸入的字符串string,index是當前匹配的字符串第一個字符在input中的位置。

  當regExp有全局標誌(g選項)時,很簡單,也符合咱們的理解:返回全部匹配到的字符串組成的數組。這是標準數組,沒有input屬性,也沒有index屬性。返回值數組中除了匹配到的字符串沒有任何其餘信息。

<script>
  var src = "The rain in Spain falls mainly in the plain.";
  var re = /\w+/g; //有g屬性。
  var i = 0;
  while (i++<10){
   arr = src.match(re);
   document.write(RegExp.index + "-" + RegExp.lastIndex + "\t" + arr + "<br/>");

  }
</script>

運行:
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain
38-43 The,rain,in,Spain,falls,mainly,in,the,plain

  從上面的分析看出,若是你只是想判斷字符串是否匹配某個正則表達式,就用test方法。若是想一次性取出全部匹配到的字符串,或者只找到第一個匹配的字符串就能夠,就用match方法。若是你想屢次匹配,並且須要知道每一個匹配到的字符串在原始字符串中的位置,或者正則表達式中還有子表達式信息須要關注,就用exec方法。

測試題:
var someText="web2.0 .net2.0";
var pattern=/(\w+)(\d)\.(\d)/g;
var outCome_exec=pattern.exec(someText);
var outCome_matc=someText.match(pattern);

What is outCome_exec[1] and outCome_matc[1]??

Choice A: true
Choice B: false
Choice C: null
Choice D: Web
Choice E: Web2.0
Choice F: undefined
Choice G: net2.0

答案爲D和G。你想明白了麼?

html

相關文章
相關標籤/搜索