Match text after colon and before new line - Javascript
本问题已经有最佳答案,请猛点这里访问。
我有以下格式的文本:
1 2 3 | Concept number 1: my content here1 Concept number 2: my content here2 Concept number 3: my content here3 |
我想要一个regext来获取
我使用的是
在线演示。
1 2 3 4 | var text = $('.text').html(); var regex = new RegExp('Concept number 1:(.*?)',"g"); var matches = text.match(regex); //matches contains `Concept number 1: my content here1` |
就我个人而言,我会在国内工作,而不是在国内工作。
1 2 3 4 5 6 7 8 | 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) |
1 2 3 4 5 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> Concept number 1: my content here1 Concept number 2: my content here2 Concept number 3: my content here3 |
你必须使用
1 2 3 4 5 | var text = $('.text').html(); var regex = new RegExp('Concept number 1:(.*?)'); var matches = regex.exec(text); console.log(matches[1]); |
1 2 3 4 5 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> Concept number 1: my content here1 Concept number 2: my content here2 Concept number 3: my content here3 |