javascript - 使用 Jquery 在字符串中查找 Html

标签 javascript jquery

我不确定这个问题有多实用,但我想做的是在字符串中找到 html,然后使用 jquery 将 pre 标签附加到它。假设我有一个带有以下字符串的变量。

var string = "Some normal text <html><body><div> This is the html </div> </body></html>";

字符串中的html是动态的,可以包含任何标签,我想要的是找到html的开始和结束并相应地追加,前置标签。

谢谢

最佳答案

下面的代码几乎可以满足您的需求,但是它不允许 html、body 标签等。但是这些在 pre 标签中是不允许的。

var string = "Some normal text <html><body><div> This is the html </div> </body></html> more text<p>more html content</p>";
var holder = $('<div>').html(string);
holder.children().wrap('<pre>');

//Print result to console
console.log(holder.html());

这里还有一个 jsFiddle:http://jsfiddle.net/evBCm/1/

关于javascript - 使用 Jquery 在字符串中查找 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9851364/

相关文章:

javascript - 在版本 6.1.8 中,单击按钮时 Revolution slider 如何再次启动(播放)?

javascript - 根据 JS 变量渲染 Grails 模板

javascript - 抓取 JS 渲染页面的方法?

javascript - 不同类型的函数声明(foo() : void{} vs foo() {}) Angular2

javascript - jquery - trim 图像以删除 div

javascript - 根据屏幕尺寸改变图像尺寸Javascript

javascript - Angular ng-repeat不更新数组

javascript - addClass 每次在每个克隆中追加

JavaScript 错误 : "Expected identifier, string or number" in IE

javascript - 使用 JS 添加到 DOM 后与 DOM 进行交互