【发布时间】:2012-11-18 03:40:58
【问题描述】:
我有一串动态填充的文本。在每个字符串中,都有一个常用词:“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);
【问题讨论】:
-
我会先写一些代码。如果它不起作用,请回到这里向我们展示您拥有的东西。
-
如果“type”出现的次数更多怎么办?
-
那么我们要抓取第一个字符串“type”
标签: javascript replace split