gpt4 book ai didi

javascript - 如何在两个字符串之间选择文本javascript?

转载 作者:行者123 更新时间:2023-12-03 03:53:45 25 4
gpt4 key购买 nike

我有这个字符串:

var str = "Toto PARKER rejected the task.<br><br>Approval comments:<br>11-07-2017 20:11:29 - James EY (Comments)<br>NO!!<br><br>";

我想要一个返回“NO!!”的函数

为此,我使用:

<p>Click the button to display the array values after the split.</p>

<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
function myFunction() {
var str = "Toto PARKER rejected the task.<br><br>Approval comments:<br>11-07-2017 20:11:29 - James EY (Comments)<br>NO!!<br><br>";
var res = str.match("(<br\s*[\/]?>)[1](.*)(<br\s*[\/]?><br\s*[\/]?>)");
document.getElementById("demo").innerHTML = res[0];
}
</script>

我的正则表达式有一些困难,因为返回是:

<br>11-07-2017 20:11:29 - James EY (Comments)<br>NO!!<br><br>

那么,如何用最后一个 <br> 开始我的正则表达式(返回<br>不!!<br><br>)以及如何不返回<br>之后?

最佳答案

这是一个没有正则表达式的解决方案。我把你的字符串分割在<br>上,然后删除 null值,并显示结果中的最后一个元素:

function myFunction() {
var str = "Toto PARKER rejected the task.<br><br>Approval comments:<br>11-07-2017 20:11:29 - James EY (Comments)<br>NO!!<br><br>";
var res = str.split('<br>').filter(n => n);
document.getElementById("demo").innerHTML = res.pop();
}
<p>Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

关于javascript - 如何在两个字符串之间选择文本javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45053474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com