Javascript:在一行字符串中获取body html.如何删除换行符
Javascript: Get body html in one row string. How to remove linebreaks?
我有html
<span style="font-family: tahoma, arial, helvetica, sans-serif;
font-size: 10pt;">One
two:</span>
我需要在一行字符串中接收html。
a = $("body").clone().find("script").remove().end().html();
a = a.replace("'r'n", "zzz");
a = a.replace("'n", "zzz");
console.log(a);
但是它不起作用。我得到的都一样。
<span style="font-family: tahoma, arial, helvetica, sans-serif;
font-size: 10pt;">One
two:</span>
用regex和全局标志替换所有新行字符:
a.replace(/'r?'n/g, ' ');
演示问题是这个字符串。replace,只删除您定义的字符串的第一个出现,您需要使用带有global
选项的regex:
a = $("body").clone().find("script").remove().end().html();
a = a.replace(/'r'n/g, " ");
a = a.replace(/'n/g, " ");
console.log(a);
VisioN
的答案更好,因为它在一个正则表达式中考虑了两种情况,使其更快。
相关文章:
- 在jQuery中使用$('body').html('')意味着什么
- Jest - querySelector('html, body') 返回奇怪的对象
- 将值从ajax传递到body html
- 自动滚动获胜't animate为$('html,body').animate
- 如何使用body onload事件操作HTML音频标记和JavaScript函数
- 在HTML中更改语言时,在body标记中添加类
- $('body').html()从来自Firefox中$jQuery.post的响应数据中给出了空白内容
- $('html, body').animate 和 $('body').animate 之间的区别
- 如何在 jQuery 中使用 $(“body”).append() 添加多个 HTML 代码
- removing html, head, body tag inside TinyMCE
- 在 HTML Body 上编写 JavaScript 变量
- HTML body onload() property
- 调用html body标记中的脚本函数
- 用从某个URL检索到的数据替换整个html-body内容
- Doctype影响html/body/canvas的宽度和高度
- HTML 'body'标签有一个onblur事件
- 在iPad上捕捉点击DOM/HTML/BODY事件
- html body ondblclick get clicked element
- Google Closure equivalent to jQuery $('html,body').a
- 使用JavaScript读取html / body标签margin-top