javascript 如何替换javascript中的特定标签内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11100077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:05:51  来源:igfitidea点击:

How to replace a specific tag content in javascript

javascriptregex

提问by seldary

I have a string, that may or may not be valid HTML, but it should contain a Title tag.
I want to replace the content of the title with new content.

我有一个字符串,它可能是也可能不是有效的 HTML,但它应该包含一个 Title 标签。
我想用新内容替换标题的内容。

Example 1:

示例 1:

lorem yada yada <title>Foo</title> ipsum yada yada  

Should turn into:

应该变成:

lorem yada yada <title>Bar</title> ipsum yada yada  

Example 2:

示例 2:

lorem yada yada <title attributeName="value">Foo</title> ipsum yada yada  

Should turn into:

应该变成:

lorem yada yada <title attributeName="value">Bar</title> ipsum yada yada  

I don't want to parse html with regex - just replace the title tag... Please don't send me here...

我不想用正则表达式解析 html - 只需替换标题标签......请不要将我发送到这里......

EDIT: After numerous down votes and a lot of patronizing attitude -
I am aware (as admitted in the original post) that usually Regex is not the way to handle HTML. I am open to any solution that will solve my problem, but till now every JQuery / DOM solution did not work. Being "right" is not enough.

编辑:经过无数次的反对票和很多光顾的态度 -
我知道(如原帖所承认的)通常正则表达式不是处理 HTML 的方式。我对任何可以解决我的问题的解决方案持开放态度,但到目前为止,每个 JQuery/DOM 解决方案都不起作用。仅仅“正确”是不够的。

回答by Qtax

It's difficultto do such a thing reliably with regex (read: "will not work for all cases"), thus using some kind of proper parser is best if possible.

这是很难做这样的事情可靠地与正则表达式(读:“将所有的情况下不工作”),从而使用某种适当的解析器是最好的,如果可能的。

That said, here is a simple expression that would work for your examples:

也就是说,这是一个适用于您的示例的简单表达式:

var re = /(<title\b[^>]*>)[^<>]*(<\/title>)/i;
str = str.replace(re, "Bar");

Some things that this does not handle and will not work right with: comments, quotes, CDATA, etc.

一些它无法处理且无法正确处理的事情:注释、引号、CDATA 等。

回答by Esailija

function replaceTitle( str, replacement ) {
    var tmp = document.createElement("ihatechrome");
    tmp.innerHTML = str;
    tmp.getElementsByTagName("title")[0].innerHTML = replacement;
    return tmp.innerHTML;   
}

replaceTitle( "lorem yada yada <title>Foo</title> ipsum yada yada", "Bar" );
//"lorem yada yada <title>Bar</title> ipsum yada yada"

For some reason, google chrome makes requests if there are imgtags with src. Doesn't make any sense but that's what happens.

出于某种原因,如果有img带有src. 没有任何意义,但这就是发生的事情。

Edit:

编辑:

This seems to work in chrome (does not load images):

这似乎适用于 chrome(不加载图像):

var doc = document.implementation.createHTMLDocument("");

doc.body.innerHTML = "<img src='/'>";

doc.body.innerHTML; //"<img src="/">"

回答by tobyodavies

Please god don't try and parse html with a regex (I know you said you aren't parsing it, but you are...) jQuery has a perfectly good set of primitives to manipulate html that isn't in the DOM:

请上帝不要尝试用正则表达式解析 html(我知道你说你没有解析它,但你是......)jQuery 有一组非常好的原语来操作不在 DOM 中的 html:

var htmlishString = "almost <title>HTML</title>";
var $fakeDiv = jQuery('<div />');
$fakeDiv.html(htmlishString).find('title').text('Bar');
var manipulatedString = $fakeDiv.html()

http://jsfiddle.net/4kQkx/

http://jsfiddle.net/4kQkx/