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...
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.
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'
becomes this:
's','e','s','s','i','o','n'
why?
The problem is probably in how you define content
.
If content=='\t session'
,
content=String(content).replace('\t','');
implies that content==' session'
.
On a side-note, the String(...)
is unnecessary.
content=content.replace('\t','');
achieves the same result.
Edit:
String(array)
does not work as you expect.
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:
var content = data.toString().replace(/\t/g, '').split('\r\n');