我有一串动态填充的文本。在每个字符串中,都有一个常用词:“type”。给定一个存储为变量的字符串,我想使用 javascript 找到单词“type”并在它之前放置一个潜水,然后在字符串之后放置一个关闭 div。像这样:
var string = "Here is some text that contains the word type and more text after";
我希望最终结果是这样的:
Here is some text that contains the word <div>type and more text after</div>
我该怎么办?
当前代码如下:
var wod = $("#wod a").attr("title");
var newwod = wod.replace("Copyright 2012, GB Blanchard - Learn Spanish: www.braser.com","");
//get ride of example and everythign after it
var finalwod = newwod.split("Example")[0];
var newstring = finalwod.replace(/(type.+)/,"<div>$1</div>");
$("#wod span").append(newstring);