从字符串javascript中删除选项卡('\t') [英] Remove tab ('\t') from string javascript

查看:118
本文介绍了从字符串javascript中删除选项卡('\t')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从javascript上的任何字符串中删除标签?

How can I remove tab from a any string on 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...

例如,我有这些行:

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

我只想要它:

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

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

by the way, when I do:

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

这就是我需要String(...)构造函数的原因。

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

如果我不使用它,生病得到对象没有方法替换。

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:

'\t session'

成为:

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

为什么?

推荐答案

问题可能在于如何定义内容

The problem is probably in how you define content.

如果 content =='\t session'

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

暗示 content =='session'

一边-note, String(...)是不必要的。

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

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

获得相同的结果。

修改:

字符串(数组)无法正常工作。

您必须在拆分字符串之前执行替换,或者单独对阵列的每个元素执行替换。

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

而不是

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

尝试

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

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

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

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

这篇关于从字符串javascript中删除选项卡('\t')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆