>  기사  >  백엔드 개발  >  javascript - 正则表达式,如何分组?地址和标题分一组

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

WBOY
WBOY원래의
2016-06-06 20:14:501100검색

<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으로 문의하세요.