gpt4 book ai didi

Javascript粗体字符串从单词到句点

转载 作者:行者123 更新时间:2023-11-28 17:07:49 27 4
gpt4 key购买 nike

假设我有这个字符串:

This has happened at it's outrageous! A big tornado might be coming towards you. But I'm not certain!

如何通过Javascript或Jquery将其一部分加粗,使其变成这样:

This has happened at it's outrageous! A big tornado might be coming towards you. But I'm not certain!

一个<b>在“ Tornado ”之前添加 </b>在 Tornado 之后的第一个周期之后添加。

规则是:“找到“ Tornado ”一词,在其前面添加<b>,并在“ Tornado ”一词后面的第一个句点后添加</b>

如果有任何帮助,我们将不胜感激!

最佳答案

下面的示例使用正则表达式和基本 DOM 操作来实现您所描述的内容。

function highlightFromWordToPeriod(words) {
let el = document.querySelector('.my-text');
el.innerHTML = el.innerHTML.replace(new RegExp(`((${words.join('|')})[^.]*.)`, 'g'), "<b>$&</b>");
}
<div class="my-text">This has happened at it's outrageous! A big tornado might be coming towards you. But I'm not certain! Watch out for hurricane as well.
</div>
<p>
<button onclick="highlightFromWordToPeriod(['tornado', 'hurricane'])">Replace</button>

关于Javascript粗体字符串从单词到句点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55316641/

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