35

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?

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
Itzik984
  • 15,968
  • 28
  • 69
  • 107

2 Answers2

40

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');
Dennis
  • 14,264
  • 2
  • 48
  • 57
  • dennis, the thing is i get content as a buffer, turning it to string by toString(); so i have to do it with the string constructor... any other idea how to handle it? – Itzik984 Jan 26 '12 at 12:32
31

You need a regexp to replace all occurrences;

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

(g being the global flag)

/^\t+/ restricts to replacing leading tabs only, /^\s+/ includes any leading whitespace which is what you would need for "\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"
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Alex, the thing is i get content as a buffer, turning it to string by toString(); so i have to do it with the string constructor... any other idea how to handle it? – Itzik984 Jan 26 '12 at 12:32
  • What is "a buffer"? And once you've used `toString`, you have a string, so still no need to use the constructor like that. – Lightness Races in Orbit Jan 26 '12 at 12:36
  • You should be able to `var str = content.toString().replace( ... );` Your example of `'s','e','s','s','i','o','n'` is the result of displaying an Array object which by default does not support `.replace` .. if `toString()` fails something is overriding/reimplementing some methods & your going to need to provide more details of `content` – Alex K. Jan 26 '12 at 12:41
  • @AlexK. i edited the answer to provide more info... would thank you a lot if u can see what is going on, if further info is needed ill give it. – Itzik984 Jan 26 '12 at 12:46
  • Can you show exactly what you pass to `translate()` what it is and where it comes from? ` – Alex K. Jan 26 '12 at 13:06
  • Can you paste the rendered web page output so we can see the raw data you work with? – Alex K. Jan 26 '12 at 13:15