javascriptRegex提取子匹配項__Regex

來源:互聯網
上載者:User

C#裡所用的Regex,如果要提取字串裡的子匹配項(我都不知道那個叫啥名字,別名。)是很方便的,比如:

Regex rx = new Regex(@"<title>(?<title>[\s\S]+)?</title>", RegexOptions.Compiled | RegexOptions.IgnoreCase);Match m = rx.Match(content);if (m.Success){    string title = m.Result("${title}");}

但如果是javascript呢,咋整。兩邊文法好像稍有點不一樣。我弄了下,只有一些支離破碎的經驗值,先記錄下來:

str = "<html><head><title>呵呵呵</title></head><body></body></html>";reg = /<title>([\s\S]+)<\/title>/givar result;if ((result = reg.exec(str)) != null) {    alert(result);//顯示“<title>呵呵呵</title>”,“呵呵呵”    alert(result[1]);//顯示“呵呵呵”    //}

參考文章:
http://www.html-js.com/article/3420

聯繫我們

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