gpt4 book ai didi

javascript - 使用 JavaScript 替换段落中的文本

转载 作者:行者123 更新时间:2023-12-02 15:51:07 24 4
gpt4 key购买 nike

对于我的应用程序,我有来自 API 的信息,这些信息是放在段落标签之间的一堆文本。然而,有时文本会显示“null”。如何使用 Javascript 删除任何包含“null”的文本并将其替换为“无信息”?类似于脏话过滤器。 enter image description here

最佳答案

只是

all_string.replace('null', 'No Info');

enter image description here

这样,您可能会将诸如 disannulling 这样的单词转换为 disanNo Infoing。因此,您应该使用正则表达式来匹配规范 null 单词:

all_string.replace(/\bnull\b/g, 'No Info');

示例

var element = document.querySelector('p');
element.innerHTML = element.innerHTML.replace(/\bnull\b/g, 'No Info');
<p>this is the first null line.

another null null null.

thisnull will not be replaced. </p>

关于javascript - 使用 JavaScript 替换段落中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31845138/

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