用Javascript替换字符串中的html
replace html in string with Javascript
我四处寻找,但没有找到答案。这是我的问题:
我从CKEDITOR中得到一个页面,其中包含:
var oldText = CKEDITOR.instances.message.getData();
到目前为止,一切都很好。字符串包含如下内容:
<table cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td align="left"
style="padding-top: 24px; padding-right: 0px; padding-bottom: 0px; padding-left: 28px;"
valign="top">
<!--//logo//-->
<a href="urlToSite" target="_blank"><img alt=""
src="urlToSite/image/data/Logo/logog7.png" /></a>
<!--//end logo//-->
</td>
<td align="right"
style="padding-top: 20px; padding-right: 28px; padding-bottom: 0px; padding-left: 0px;"
valign="top">
<div style="font-size: 18px; color: rgb(53, 115, 173);">Monday, 29
July 2013</div>
<div style="font-size: 11px; color: rgb(53, 115, 173);">
<a
href="http://urlToSite/index.php?route=ne/subscribe&uid={uid}&key={key}"
style="text-decoration: none; color: rgb(53, 115, 173);">Subscribe</a> | <a
href="http://urlToSite/index.php?route=ne/unsubscribe&uid={uid}&key={key}"
style="text-decoration: none; color: rgb(53, 115, 173);">Unsubscribe</a> | <a
href="urlToSite"
style="text-decoration: none; color: rgb(53, 115, 173);">Visit our
site</a>
</div>
</td>
</tr>
</tbody>
</table>
现在我想把取消订阅链接改成这样吗(这发生在switch语句中):
http://urlToSite/index.php?route=ne/unsubscribe&uid={uid}&key={key}&c=1&l=12
(注意,l参数是动态的)。但我也不想再让他回到原来的网址。
我如何在字符串中搜索该URL(有没有这些额外的参数,这取决于)并替换它
我对此一无所知,但如果你们能帮助我,甚至为我指明一点正确的方向,那会很棒吗。
所以在某种程度上,这就是我想要实现的:
- 我有这个URL:
http://urlToSite/index.php?route=ne/unsubscribe&uid={uid}&key={key}
- 我想将其更改为以下网址:
http://urlToSite/index.php?route=ne/unsubscribe&uid={uid}&key={key}&c=1&i5
- 那么我不想让url变成这样:
http://urlToSite/index.php?route=ne/unsubscribe&uid={uid}&key={key}&c=1&i=2
- 但我也希望无法将URL恢复正常(我也可以在静态变量中这样做,所以这没什么大不了的…)
-顺便说一句,抱歉英语不好;)
编辑
你们认为以下是解决这个问题的好方法吗???
1: I save the default URL in an string
2: I do an replace with the default URL on the oldText, and insert the new URL
3: I save the new URL in an difrent string
4: Next time i do an replace with the saved new URL on the oldText and insert an newer URL
5: and save that and so on...
编辑2我现在使用的代码如下(答案来自Woody)
var oldText = CKEDITOR.instances.message.getData();
var div = document.createElement("div");
div.innerHTML = oldText;
var a = div.querySelector('a[href^="http://www.gospel7.com/index.php?route=ne/unsubscribe"]');
$(a).attr("href", "http://urlToSite/index.php?route=ne/unsubscribe&uid={uid}&key={key}&c=1&l=12/")
console.log(a);
获取HTML字符串并将其放入内存中的div中,这样您就可以对其进行操作。
vardiv=document.create元素("div");
div.innerHTML = oldText;
现在您有了一个div,可以查询anchor标记。
var a = div.querySelector("a[href^=<start of URL you want to change] ");
现在将锚点上的href设置为您想要的。完成后获取div的innerHTML。
var oldURL = "http://urlToSite/index.php?route=ne/subscribe&uid={uid}&key={key}";
var newURL = "http://urlToSite/index.php?route=ne/unsubscribe&uid={uid}&key={key}&c=1&l=12"
var newText = oldText.replace("oldURL", "newURL");
您只是在进行查找和替换(同时存储当前变量)吗?
如果您需要更动态的东西,请查看正则表达式http://www.regular-expressions.info/reference.html
相关文章:
- 将html元素插入到文本字符串中,以匹配另一个html字符串
- HTML字符串作为上下文
- 正在分析HTML字符串并从中进行选择
- 将GET请求(HTML字符串)转换为完整的DOM对象
- 在树枝模板上从数据库中解压缩HTML字符串
- 如何使用Razor显示模板将字符串转换为html字符串
- 如何在不打断标记的情况下突出显示html字符串中的文本
- 使用val()在jquery循环中设置html字符串的val
- 搜索并用jQuery替换整个HTML字符串
- 替换 html 字符串中的某些“<”和“>”
- Javascript-将包含变量的html字符串作为参数传递
- 如何用JavaScript替换HTML字符串
- 如何在我的模板(Angular2/TypeScript)中将HTML字符串转换为HTML
- 在HTML字符串的某个段落后插入HTML
- 使用 javascript 压缩包含嵌入图像的 HTML 字符串
- 需要使用Java编码/转义HTML字符串,并在JavaScript中解码HTML字符串
- 在不更改 HTML 的情况下操作 HTML 字符串的内容
- 使用JavaScript从HTML字符串中提取文本
- regex在JavaScript中拆分一个html字符串
- 创建HTML对象的Javascript与创建HTML字符串的比较