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

    Replace multiple single quotes in string

    Hi Frinds,

    I have a string
    --------------
    saravanan \' krish

    here i tried to replace the \' with %% like below.
    -------------------------------------------------------------
    NotificationTitle.replace("\'", "%%") its working fine.


    But now the string was
    -----------------------
    saravanan \' krish \' logu \' mani \'

    the out put was
    ----------------
    saravanan %% krish \' logu \' mani \'


    I couldnt replace remain \' characters with the %%....when i having multiple single quotes am not able to replace more then one \' how to replace all \' with %%
    how to achieve via javascript????


    Thanks
  • #755339
    Hi,

    I tried in the below manner for me it's working fine.


    string str = "saravanan \' krish \' logu \' mani \' ";
    str=str.Replace("\'","%%");


    use "\'" if you are missed single code then it is wrong why because black slash indicates the new line, it won't work.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #755359
    Please try the code as shown in the following sample code:

    <asp:Label runat="server" ID="lblData" Text="saravanan \' krish \' logu \' mani \' "></asp:Label>
    <asp:Button ID="Button1" OnClientClick="return Test();" runat="server" Text="Button" />

    <script type="text/javascript" >
    function Test()
    {
    var val = document.getElementById("lblData").innerHTML;
    val = val.replace(/\\'/g, "%%");
    document.getElementById("lblData").innerHTML = val;
    return false;
    }
    </script>

    in the above script code, /g is used to replace all occurrences of the specified character "\'" with the "%%" character.

    Please update the code as per your requirement. it should work.

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #755437
    You need to use strings with replaceAll to Replace multiple single quotes in strin your String. Example of repalceall
    str = str.replaceAll("'","''");


  • Sign In to post your comments