Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  156] [ 5]  / answers: 1 / hits: 18284  / 12 Years ago, thu, august 9, 2012, 12:00:00

I have a function f similar to



function f(str){
alert(abc+str);
}


Now, I want to use JavaScript special charecter b in such a way that I can choose if I want to display the hardcoded string abc or not. For example,



f(bb+yz); //should output ayz


I tried the same, but it does not work. In other words, I want to concat a string with a backspace character so that I can remove last characters from the string.



Can we do this in JavaScript?



EDIT
The real code is too much big (its a HUGE 1 liner that concats many many strings). To map that in above example, we cannot edit the function f, so do whatever you want from outside function f.


More From » string

 Answers
34

The problem comes from the fact that b is just another character in the ASCII code. The special behaviour is only when implemented by some string reader, for example, a text terminal.



You will need to implement the backspace behaviour yourself.



function RemoveBackspaces(str)
{
while (str.indexOf(b) != -1)
{
str = str.replace(/.?x08/, ); // 0x08 is the ASCII code for b
}
return str;
}


Example: http://jsfiddle.net/kendfrey/sELDv/



Use it like this:



var str = RemoveBackspaces(f(bbyz)); // returns ayz

[#83726] Wednesday, August 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
talonb

Total Points: 596
Total Questions: 103
Total Answers: 91

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
;