You must Sign In to post a response.
  • Category: JavaScript

    Replace special character in javascript

    Hi Friends,


    I have following string getting inmy javascrpt file.
    "t\'t"

    I want to replace the \' with ' as opuput like below

    "t't"

    How to achiveve with javascript ???

    Thx in advance..
  • #755269
    Hi,

    use replace method for achieve ur target like:

    var str = "t\'t";
    var res = str.replace("\'", "");


    try it once, i think it will work for uu.

    Thanks,
    chitaranjan

  • #755276
    Please check the sample code below to replace special character.



    <script language="JavaScript">

    var tmp = new String('This is sample te!!!!st st>ring... So??? What...');

    document.write(tmp + '<br>');

    tmp = tmp.replace(/[^a-zA-Z 0-9]+/g,'');

    document.write(tmp + '<br>');

    </script>

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #755277
    Hi Thx chitaranjan.

    But the opuput as per your solution was.The single quote also replaced

    "tt"

    I want output like

    "t't"

    Thanks

  • #755278
    You need to use RegExp for replace special characters through JavaScript . Example
    	
    str = str.replace(new RegExp('[^a-z0-9'+rep+']',"g"), '')
    .replace(/-+/g, rep);

    return str;
    }

  • #755283
    Hi,

    Then use the code like:

    var str = "t\'t";
    var res = str.replace("\", "");


    now try , removing the single quote(')


    Thanks,
    chitaranjan

  • #755337
    Thx all friends


  • Sign In to post your comments