我有一个动态填充的文本字符串。在每一个字符串中,都有一个常用词:“type”。给定一个存储为变量的字符串,我想用javascript来查找单词“type”,并在它之前放置一个潜水,然后在该字符串之后关闭div。 像这样:使用javascript将字符串转换为dom元素
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);
我会写一些代码开始。当它不起作用时,回到这里并告诉我们你有什么。 –
如果“类型”出现的次数更多,该怎么办? –
然后我们要抓住第一个字符串“type” – JCHASE11