Match text after colon and before new line
This question already has an answer here:
You have to use regex.exec()
instead of .match()
. Then you can access the matching groups as an ordinary array:
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/76798.html
上一篇: python重新匹配在JavaScript中相当于
下一篇: 匹配冒号后和新行之前的文本