![](/img/trans.png)
[英]Reg. exp. in javascript to get last <a> tag in substring
[英]Javascript reg exp between closing tag to opening tag
如何使用正则表达式选择</h2>
结束标记之后的文本,直到下一个<h2>
开始标记
<h2>my title here</h2>
Lorem ipsum dolor sit amet <b>with more tags</b>
<h2>my title here</h2>
consectetur adipisicing elit quod tempora
在这种情况下,我想选择此文本: Lorem ipsum dolor sit amet <b>with more tags</b>
试试这个: /<\\/h2>(.*?)</g
这将找到一个结束标签,然后在新的开始标签之前捕获任何内容。
在 JS 中,您可以这样做以获取文本:
substr = str.match(/<\/h2>(.*?)<h2/)[1];
var str = '<h2>my title here</h2>Lorem ipsum <b>dolor</b> sit amet<h2>my title here</h2>consectetur adipisicing elit quod tempora'; var substr = str.match(/<\\/h2>(.*?)<h2/)[1].replace(/<.*?>/g, ''); console.log(substr); //returns: Lorem ipsum dolor sit amet
尝试
/<\/h2>((?:\s|.)*)<h2/
你可以在这个 regex tester 上看到它的作用。
您也可以在下面的示例中看到它。
(function() { "use strict"; var inString, regEx, res, outEl; outEl = document.getElementById("output"); inString = "<h2>my title here</h2>\\n" + "Lorem ipsum dolor sit amet <b>with more tags</b>\\n" + "<h2> my title here </h2>\\n" + "consectetur adipisicing elit quod tempora" regEx = /<\\/h2>((?:\\s|.)*)<h2/ res = regEx.exec(inString); console.log(res); res.slice(1).forEach(function(match) { var newEl = document.createElement("pre"); newEl.innerHTML = match.replace(/</g, "<").replace(/>/g, ">"); outEl.appendChild(newEl); }); }());
<main> <div id="output"></div> </main>
我在您的示例中添加了\\n
以模拟新行。 不知道为什么您不只是使用querySelector()
选择<h2>
并以这种方式获取文本。
使用 string replace()
函数匹配标签并删除它们。 此外,这个提议的解决方案removes any single closure tags like <br/>,<hr/>
等
var htmlToParse = document.getElementsByClassName('input')[0].innerHTML; var htmlToParse = htmlToParse.replace(/[\\r\\n]+/g,""); // clean up the multiLine HTML string into singleline var selectedRangeString = htmlToParse.match(/(<h2>.+<h2>)/g); //match the string between the h2 tags var parsedString = selectedRangeString[0].replace(/((<\\w+>(.*?)<\\/\\w+>)|<.*?>)/g, ""); //removes all the tags and string within it, Also single tags like <br/> <hr/> are also removed document.getElementsByClassName('output')[0].innerHTML += parsedString;
<div class='input'> <i>Input</i> <h2>my title here</h2> Lorem ipsum dolor sit amet <br/> <b>with more tags</b> <hr/> <h2>my title here</h2> consectetur adipisicing elit quod tempora </div> <hr/> <div class='output'> <i>Output</i> <br/> </div>
代码中需要记住的几件事。
htmlToParse.match(/(<h2>.+<h2>)/g);
返回一个字符串数组,即与此正则表达式匹配的所有字符串。
selectedRangeString[0]
我只是将第一个匹配项用于演示目的。 如果你想玩所有的字符串,那么你可以用相同的逻辑循环它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.