JavaScript concat string with backspace

后端 未结 4 665
清酒与你
清酒与你 2020-12-17 20:18

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

4条回答
  •  清歌不尽
    2020-12-17 20:35

    Something like this:

    function f(str, abc){
       if(!abc) abc = "abc";
       if (str.indexOf("\b") != "undefined")
       {
           abc = abc.slice(0,-1);
           str = str.replace("\b","");
           f(str, abc);
       }
       else alert(abc+str);
    }
    

    and as an added bonus you get to use recursion!

    note that this is a little slower than doing it this way:

    function f(str){
        var count = 0;
        var abc = "abc";
        for(var i = 0; i < str.length; i++)
        { 
           if(str[i] = "\b") //at least i think its treated as one character...
           count++;
        }
        abc = abc.slice(0, count * -1);
        alert(abc+str);
    }
    

提交回复
热议问题