js中的Regex(1)

來源:互聯網
上載者:User

標籤:規則   正則   種類   運算式   bsp   大小   作用   標識   構造器   

定義Regex規則對象有兩種:

1,var reg = /\bis\b/g;   這種情況下\轉義作用,\b空格   /*/中的內容就是正則規則

2,var reg = new RegExp("\\bis\\b","g");   有兩個\\是因為在js的字串中\本身也是字元需要轉義  通過這種類似構造器的方式定義正則時不需要開始和結尾的//;

舉例:

"he is a boy is he".replace(/\bis\b/,"am");
"he am a boy is he"

 

var reg = new RegExp("\\bis\\b","g");
"he is a boy is he".replace(reg,"am");
"he am a boy am he"

字串的replace方法提供的參數中,第一個參數是正則對象,第二個是要替換成的字元

3中標識符,g全文檢索搜尋  i忽略大小寫  m多行搜尋

舉例:

var reg = new RegExp("\\bis\\b","");
"he is a boy is he".replace(reg,"am");
"he am a boy is he"

"he is a boy; IS he".replace(/\bis\b/gi,"am");
"he am a boy; am he"

"he is a boy; IS he".replace(/\bis\b/g,"am");
"he am a boy; IS he"

 

   

js中的Regex(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.