首頁 >後端開發 >php教程 >javascript - 正则表达式,如何分组?地址和标题分一组

javascript - 正则表达式,如何分组?地址和标题分一组

WBOY
WBOY原創
2016-06-06 20:14:501125瀏覽

<code>http://s.click.taobao.com/t?e=m%3D2%26s%3DC1Wj%2FDUvnQAcQipKwQzePOeEDrYVVa64REOHN%2B0iJT23bLqV5UHdqcvC2rKjuw6htYQMu9Tydq0qC%2FWAES3KjEVAR5FF1Zuf0M83nhvB7wsgfuBp9F3RPL6hW3GYyufa0nk1uZWo%2BVgFCjfdoeI9bCSQFfUF8UG8x1IYFu75azTnWUvVnK10NV9FS%2FIENZ7iyNYJBoTNJ8s%3D
创意男女雨伞长柄伞晴雨伞双人伞超大拍下19.8元包邮
http://s.click.taobao.com/t?e=m%3D2%26s%3D7hgtkzLk%2FnkcQipKwQzePOeEDrYVVa64qu1K02Mshg63bLqV5UHdqcvC2rKjuw6h0e71iVTN2RwqC%2FWAES3KjEVAR5FF1Zuf0M83nhvB7wsgfuBp9F3RPP0sHo1DWd7dVEdXKgUyOXuT1UqJ0PWIzMZOr18RYav6TWOYZ4V%2FN1d%2FC3KL%2B7ueeV1VbYMvpJWxTM78ni%2FxOXSiZ%2BQMlGz6FQ%3D%3D
佳诺仰卧板仰卧起坐健身器材仅78元起包邮
</code>

上面的文本,如何变成2组,每组包括地址和标题?
我用下面的正则匹配出了5组搞了半天没搞出来

javascript - 正则表达式,如何分组?地址和标题分一组

回复内容:

<code>http://s.click.taobao.com/t?e=m%3D2%26s%3DC1Wj%2FDUvnQAcQipKwQzePOeEDrYVVa64REOHN%2B0iJT23bLqV5UHdqcvC2rKjuw6htYQMu9Tydq0qC%2FWAES3KjEVAR5FF1Zuf0M83nhvB7wsgfuBp9F3RPL6hW3GYyufa0nk1uZWo%2BVgFCjfdoeI9bCSQFfUF8UG8x1IYFu75azTnWUvVnK10NV9FS%2FIENZ7iyNYJBoTNJ8s%3D
创意男女雨伞长柄伞晴雨伞双人伞超大拍下19.8元包邮
http://s.click.taobao.com/t?e=m%3D2%26s%3D7hgtkzLk%2FnkcQipKwQzePOeEDrYVVa64qu1K02Mshg63bLqV5UHdqcvC2rKjuw6h0e71iVTN2RwqC%2FWAES3KjEVAR5FF1Zuf0M83nhvB7wsgfuBp9F3RPP0sHo1DWd7dVEdXKgUyOXuT1UqJ0PWIzMZOr18RYav6TWOYZ4V%2FN1d%2FC3KL%2B7ueeV1VbYMvpJWxTM78ni%2FxOXSiZ%2BQMlGz6FQ%3D%3D
佳诺仰卧板仰卧起坐健身器材仅78元起包邮
</code>

上面的文本,如何变成2组,每组包括地址和标题?
我用下面的正则匹配出了5组搞了半天没搞出来

javascript - 正则表达式,如何分组?地址和标题分一组

先把最后的"\n"去掉啊,不过既然是分行的,干嘛不s.split("\n")

就这样分组就行,但是要遍历析取,每次取出两组,用javascript实现:

<code class="js">var str = 'http://s.click.taobao.co/t?e=m=2...',
  matchArr,
  REGEX = /(.*)([\u4e00-u9fa5]*)\n?/g;

while ((matchArr = REGEX.exec(str))) {
  var address = matchArr[1], 
    title = matchArr[2];

  console.log(address);
  //http://s.click.taobao.com/t?e=m=2...

  console.log(title);
  //创意男女雨伞长柄伞...
}</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn