Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove tab ('\t') from string 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...

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?

like image 417
Itzik984 Avatar asked Jan 26 '12 12:01

Itzik984


People also ask

How do you remove T from string?

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

How do I remove a specific part of a string in Javascript?

To remove all occurrences of a substring from a string, call the replaceAll() method on the string, passing it the substring as the first parameter and an empty string as the second. The replaceAll method will return a new string, where all occurrences of the substring are removed.

How do you remove a line break in a string?

Use the String. replace() method to remove all line breaks from a string, e.g. str. replace(/[\r\n]/gm, ''); . The replace() method will remove all line breaks from the string by replacing them with an empty string.

Does Javascript trim remove newline?

trim method removes any line breaks from the start and end of a string. It handles all line terminator characters (LF, CR, etc). The method also removes any leading or trailing spaces or tabs. The trim() method doesn't change the original string, it returns a new string.


2 Answers

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');
like image 85
Dennis Avatar answered Oct 07 '22 20:10

Dennis


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"
like image 43
Alex K. Avatar answered Oct 07 '22 20:10

Alex K.