【发布时间】:2013-09-06 14:01:36
【问题描述】:
我在 JS 字符串操作方面有一个具有挑战性的任务: 有一个 HTML 字符串,我需要在其中替换特定单词索引处的单词。单词索引是忽略 HTML 标记时单词的编号。
例如,这里是 HTML 字符串:
<span style="font-family:Times New Roman;">At times like this I don't know what to
do. Other times I have no problem.</span>
任务是将html中的第2个单词(即单词times)替换为文本:<span style="color:red;">times</span>
所以最终的 HTML 字符串应该是:
<span style="font-family:Times New Roman;">At <span style="color:red;">times</span>
like this I don't know what to do. Other times I have no problem.</span>
对这个挑战有什么想法吗?
【问题讨论】:
-
你想替换所有出现的那个词还是只替换第一个?
标签: javascript html string replace