代码之家  ›  专栏  ›  技术社区  ›  ujjwal verma

从文本字符串中选择多个关键字[重复]

  •  0
  • ujjwal verma  · 技术社区  · 8 年前

    var hashtag = $('p').text().match(/#\w+\s/);
    console.log(hashtag);
    

    .match() 函数来查找定义的regex表达式的匹配项,但它只显示一个关键字,而我有两个关键字。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Dinesh Ghule    8 年前

    var hashtag = $('p').text().match(/#\w*\s*/gi);
    console.log(hashtag);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p>#this is #the#text with#regular#expression hash #tags</p>
        2
  •  2
  •   BenM    8 年前

    g 标记到您的regex( /#\w+\s/g

    var hashtag = $('p').text().match(/#\w+\s/g);
    console.log(hashtag);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p>
    This is some text that #has #hash #tags
    </p>