替换 JavaScript 中的制表符

发布于 2024-10-09 10:10:34 字数 442 浏览 0 评论 0原文

请考虑以下 HTML

元素:

This is some  
example code which    
     contains tabs

我想用 HTML 中的四个不间断空格字符替换所有制表符(即  )。我已经用 JavaScript 测试了上面的 pre 元素是否存在制表符,如下所示:

$('pre').ready(function() {
    alert(/\t/.test($(this).text()));
});

但它总是返回 false。谁能告诉我将源代码中的制表符空格替换为 HTML NBSP 的正确过程?这些选项卡已由 Komodo Edit 添加,并且在查看源代码时可见。

Please consider the following HTML <pre> element:

This is some  
example code which    
     contains tabs

I would like to replace all of the tab characters with four non-breaking space characters in HTML (i.e.  ). I have tested the above pre element with JavaScript for the presence of tab characters as follows:

$('pre').ready(function() {
    alert(/\t/.test($(this).text()));
});

But it is always returned false. Can anyone tell me the correct process by which to replace tab spaces from the source code to HTML NBSPs? The tabs have been added by Komodo Edit, and are visible when viewing the source.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江南烟雨〆相思醉 2024-10-16 10:10:34

您可以这样做:

$('pre').html(function() {
    return this.innerHTML.replace(/\t/g, '    ');
});

这将循环访问页面上的所有 pre 元素并为每个元素调用函数。 jQuery的html函数使用我们给出的函数的返回值来替换每个元素的内容。我们使用 String#replace 将 HTML 字符串中的所有制表符(请注意正则表达式上的 g 标志)替换为四个不间断空格。

实例

You can do it like this:

$('pre').html(function() {
    return this.innerHTML.replace(/\t/g, '    ');
});

That will loop through all pre elements on the page and call the function for each of them. jQuery's html function uses the return value of the function we give to replace the content of each element. We're using String#replace to replace all (note the g flag on the regexp) tab characters in the HTML string with four non-breaking spaces.

Live example

我乃一代侩神 2024-10-16 10:10:34

它删除换行符、多余空格和换行符:

function removeNewlines(str) {
//remove line breaks from str
str = str.replace(/\s{2,}/g, ' ');
str = str.replace(/\t/g, ' ');
str = str.toString().trim().replace(/(\r\n|\n|\r)/g,"");
console.log(str);
}

演示:

function removeNewlines(str) {
//remove line breaks from str
str = str.replace(/\s{2,}/g, ' ');
str = str.replace(/\t/g, ' ');
str = str.toString().trim().replace(/(\r\n|\n|\r)/g,"");
  console.log(str);
}

$('#acceptString').click(function() {
    var str = prompt('enter string','');
    if(str)
        removeNewlines(str)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' value='Enter String' id='acceptString' />

It removes line breaks, extra spaces and line breaks:

function removeNewlines(str) {
//remove line breaks from str
str = str.replace(/\s{2,}/g, ' ');
str = str.replace(/\t/g, ' ');
str = str.toString().trim().replace(/(\r\n|\n|\r)/g,"");
console.log(str);
}

Demo:

function removeNewlines(str) {
//remove line breaks from str
str = str.replace(/\s{2,}/g, ' ');
str = str.replace(/\t/g, ' ');
str = str.toString().trim().replace(/(\r\n|\n|\r)/g,"");
  console.log(str);
}

$('#acceptString').click(function() {
    var str = prompt('enter string','');
    if(str)
        removeNewlines(str)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' value='Enter String' id='acceptString' />

渡你暖光 2024-10-16 10:10:34

试试这个:

var tab = RegExp("\\t", "g");
document.getElementById("text").value =
document.getElementById("text").value.replace(tab,'    ');

Try this:

var tab = RegExp("\\t", "g");
document.getElementById("text").value =
document.getElementById("text").value.replace(tab,'    ');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文