Javascript 从字符串javascript中删除选项卡('\ t')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9018015/
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-08-24 08:24:38  来源:igfitidea点击:

Remove tab ('\t') from string javascript

javascriptstringparsing

提问by Itzik984

How can I remove tab from a any string on javascript?

如何从javascript上的任何字符串中删除选项卡?

when I get my string it comes as a buffer like this:

当我得到我的字符串时,它是一个像这样的缓冲区:

<Buffer 0d 0a 3c 25 72 65 73 70 6f 6e 73 65 2e 73 74 61 74 75 73 20...>

 function translate(data) {

  var content = data.toString().split('\r\n');

  }

and then I perform the following...

然后我执行以下...

for example, I have these lines:

例如,我有这些行:

 '\t\t var session = request.getSession();'
 '\t\t session["user"] = {};'

and I just want it to be:

我只是希望它是:

'var session = request.getSession();'
'session["user"] = {};'

by the way, when I do:

顺便说一句,当我这样做时:

content=String(content).replace('\t','');

this is why I need the String(...) constructor.

这就是为什么我需要 String(...) 构造函数。

if I wont use it, ill get the object has no method replace.

如果我不使用它,就会导致对象没有方法替换。

assuming content is the string i want to parse it parses it by letter meaning this:

假设 content 是我想解析的字符串,它按字母解析它,意思是:

'\t session'

becomes this:

变成这样:

's','e','s','s','i','o','n'

why?

为什么?

回答by Dennis

The problem is probably in how you define content.

问题可能在于您如何定义content.

If content=='\t session',

如果content=='\t session',

`content=String(content).replace('\t','');`

implies that content==' session'.

暗示content==' session'.

On a side-note, the String(...)is unnecessary.

附带说明一下,String(...)是不必要的。

`content=content.replace('\t','');`

achieves the same result.

达到相同的结果。

Edit:

编辑:

String(array)does not work as you expect.

String(array)不像你期望的那样工作。

You have to either perform the replace before you split the string or perform the replace on every element of the array separately.

您必须在拆分字符串之前执行替换,或者分别对数组的每个元素执行替换。

Instead of

代替

var content = data.toString().split('\r\n');
content=String(content).replace('\t','');

try

尝试

var content = data.toString().replace('\t', '').split('\r\n');

Note that replace('\t', '')will replace only the first occurrence of \t. To do a global replace, use the RegExp Alex K. suggested:

请注意,replace('\t', '')将仅替换第一次出现的\t. 要进行全局替换,请使用 RegExp Alex K. 建议:

var content = data.toString().replace(/\t/g, '').split('\r\n');

回答by Alex K.

You need a regexp to replace all occurrences;

你需要一个正则表达式来替换所有的出现;

content = content.replace(/\t/g, '');

(g being the global flag)

(g 是全球标志)

/^\t+/restricts to replacing leadingtabs only, /^\s+/includes any leading whitespace which is what you would need for "\t\t var" -> "var"

/^\t+/仅限替换前导标签,/^\s+/包括您需要的任何前导空格"\t\t var" -> "var"

Update

更新

You haven't said how the buffer is received & what type it is, my closest guess although its a strange thing to be receiving;

你还没有说缓冲区是如何接收的以及它是什么类型,我最接近的猜测,尽管接收是一件奇怪的事情;

var test_buffer_array = "\x0d \x0a \x3c \x25 \x72 \x65 \x73 \x70 \x6f \x6e \x73 \x65 \x2e \x73 \x74 \x61 \x74 \x75 \x73 \x20".split(" ")

translate(test_buffer_array);

function translate(data) {
    var content = data.join("").replace(/^\t+/gm, '');
    print(content);
}

result: "<%response.status"