Remove tab ('\t') from string javascript
Solution 1
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');
Solution 2
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"
Itzik984
Updated on October 11, 2021Comments
-
Itzik984 over 2 years
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?
-
Itzik984 over 12 yearsdennis, 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 over 12 yearsAlex, 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?
-
Lightness Races in Orbit over 12 yearsWhat is "a buffer"? And once you've used
toString
, you have a string, so still no need to use the constructor like that. -
Alex K. over 12 yearsYou 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
.. iftoString()
fails something is overriding/reimplementing some methods & your going to need to provide more details ofcontent
-
Itzik984 over 12 years@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.
-
Alex K. over 12 yearsCan you show exactly what you pass to
translate()
what it is and where it comes from?<Buffer 0d 0a 3c 25 72
is not very descriptive unless its a server side thing ... -
Alex K. over 12 yearsCan you paste the rendered web page output so we can see the raw data you work with?