每天一篇javascript學習小結(RegExp對象),javascriptregexp

來源:互聯網
上載者:User

每天一篇javascript學習小結(RegExp對象),javascriptregexp

1、Regextest方法

var text = "cat, bat, sat, fat";    var pattern = /.at/;    if (pattern.test(text)){   alert("The pattern was matched.");  }

2、正則的toString()方法

var pattern = new RegExp("\\[bc\\]at", "gi");  alert(pattern.toString()); // /\[bc\]at/gi  alert(pattern.toLocaleString()); // /\[bc\]at/gi

3、RegExp Constructor(建構函式) Properties(屬性)

 var text = "this has been a short summer";  var pattern = /(.)hort/g;    /*   * Note: Opera doesn't support input, lastMatch, lastParen, or multiline.   * Internet Explorer doesn't support multiline.   */    if (pattern.test(text)){   alert(RegExp.input);    //this has been a short summer   alert(RegExp.leftContext);   //this has been a      alert(RegExp.rightContext);  // summer   alert(RegExp.lastMatch);   //short   alert(RegExp.lastParen);   //s   alert(RegExp.multiline);   //false  }  input      儲存被搜尋的字串  index      儲存匹配的首字元的位置  lastIndex     儲存匹配的字串下一個字元的位置  lastMatch     儲存匹配到的字串  lastParen     儲存最後一個被匹配的字串(最後一個括弧內的內容)  leftContext    儲存匹配字串左邊的內容  rightContext   儲存匹配字串右邊的內容  $1~$9   儲存最開始的9個子匹配(括弧中的內容)
 var text = "this has been a short summer";  var pattern = /(.)hort/g;    /*   * Note: Opera doesn't support short property names.   * Internet Explorer doesn't support multiline.   */    if (pattern.test(text)){   alert(RegExp.$_);    //this has been a short summer   alert(RegExp["$`"]);   //this has been a      alert(RegExp["$'"]);   // summer   alert(RegExp["$&"]);   //short   alert(RegExp["$+"]);   //s   alert(RegExp["$*"]);   //false  }  * 分為長屬性名稱和短屬性名稱  * input   $_  最近一次要匹配的字串  * lastMatch  $&  最近一次的匹配項  * lastParen  $+  最近一次匹配的擷取的群組  * leftContext  $`  input字串中lastMatch之前的文本  * multiline  $*  布爾值,表示是否所有運算式都使用多行模式。  * rightContext $'  input字串中lastMatch之後的文本

4、正則$1.....$9

 var text = "this has been a short summer";  var pattern = /(..)or(.)/g;      if (pattern.test(text)){   alert(RegExp.$1);  //sh   alert(RegExp.$2);  //t  }  每當產生一個帶括弧的成功匹配時,$1...$9 屬性的值就被修改。   可以在一個Regex模式中指定任意多個帶括弧的子匹配,但只能儲存最新的九個。

5、RegExp exec() 

var text = "mom and dad and baby";    var pattern = /mom( and dad( and baby)?)?/gi;  var matches = pattern.exec(text);    alert(matches.index); //0 第一個被匹配到的位置  alert(matches.input); //"mom and dad and baby" 匹配的原始字串  alert(matches[0]);  //"mom and dad and baby" 匹配的第一個值  alert(matches[1]);  //" and dad and baby" 匹配的第二個值  alert(matches[2]);  //" and baby"   匹配的第三個值
 var text = "cat, bat, sat, fat";    var pattern1 = /.at/;    var matches = pattern1.exec(text);    alert(matches.index); //0  alert(matches[0]);  //"cat"  alert(pattern1.lastIndex);//0  matches = pattern1.exec(text);    alert(matches.index); //0  alert(matches[0]);  //"cat"  alert(pattern1.lastIndex);//0  var pattern2 = /.at/g;    var matches = pattern2.exec(text);    alert(matches.index); //0  alert(matches[0]);  //"cat"  alert(pattern2.lastIndex);//0  matches = pattern2.exec(text);    alert(matches.index); //5  alert(matches[0]);  //"bat"  alert(pattern2.lastIndex);//0

6、RegExp執行個體屬性

 var pattern1 = /\[bc\]at/i;    alert(pattern1.global);  //false //是否設定全域尋找  alert(pattern1.ignoreCase); //true 是否忽略大小寫  alert(pattern1.multiline); //false 是否設定多行尋找  alert(pattern1.lastIndex); //0  一個整數,標示開始下一次匹配的字元位置。  alert(pattern1.source);  //"\[bc\]at" Regex的源文本。  var pattern2 = new RegExp("\\[bc\\]at", "i");    alert(pattern2.global);  //false  alert(pattern2.ignoreCase); //true  alert(pattern2.multiline); //false  alert(pattern2.lastIndex); //0  alert(pattern2.source);  //"\[bc\]at"

以上就是今天的javascript學習小結,之後每天還會繼續更新,希望大家繼續關注。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.