匹配冒号后和新行之前的文本

这个问题在这里已经有了答案:

  • 如何访问JavaScript正则表达式中的匹配组? 14个答案

  • 您必须使用regex.exec()而不是.match() 。 然后您可以像普通数组那样访问匹配的组:

    var text = $('.text').html();
    var regex = new RegExp('Concept number 1:(.*?)<br>');
    var matches = regex.exec(text);
    
    console.log(matches[1]);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="text">
    Concept number 1: my content here1<br>
    Concept number 2: my content here2<br>
    Concept number 3: my content here3<br>
    </div>

    就我个人而言,我会努力解决DOM,而不是innerHTML

    var text = $('.text').contents()  //get all the textnodes and elements
                 .filter( function (i, node) { //get just the text nodes
                   return node.nodeType==3
                 }).map( function (i, node) {  //get the text you are after
                   var match = node.nodeValue.match(/:s+(.*)/)
                   return match ? match[1] : undefined
                }).get()  //turn jQuery object into array
    console.log(text)
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="text">
    Concept number 1: my content here1<br>
    Concept number 2: my content here2<br>
    Concept number 3: my content here3<br>
    </div>
    链接地址: http://www.djcxy.com/p/76797.html

    上一篇: Match text after colon and before new line

    下一篇: JavaScript RegExp matching group takes unwanted parentheses