【发布时间】:2017-01-08 17:14:21
【问题描述】:
我已经用 jQuery 构建了一个小的查找和替换功能。 (See functional example here) 在您考虑格式化标签之前,它运行良好。
假设以下html:
Here <i>is</i> my <b>sample</b> text
如果您搜索sample text,搜索会因为粗体标签而失败。
可接受的结果是删除<b>sample</b> text,将其替换为您输入的任何替换内容。它应该单独留下斜体标签。这怎么可能实现?
这是我目前所拥有的:
function findandreplace() {
var text = $('.editor').html(),
find = $('#find').val(),
replace = $('#replace').val(),
newText = text.replace(new RegExp(find, "g"), replace)
$('.editor').html(newText);
}
【问题讨论】:
-
看看这个:jsfiddle.net/u6fef1eg/5你可以用斜体或粗体替换单词
-
@MoolsBytheway,似乎对我不起作用?为了清楚起见,我在搜索
sample text时需要替换<b>sample</b> text。
标签: javascript jquery replace find