Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replace occurance(s) of ' with \'

    I'm trying to find an easy way to search a string and if a single quote is found, replace it with \'

    So:

    var a = "It's doing my head in!"

    OR

    var a = "I can't do it - it's too hard."


    How can I turn either of those into:

    It\'s doing my head in!

    OR

    I can\'t do it - it\'s too hard.


    Cheers.


    Edit: I cant just use:

    var a = "I can\'t do it - it\'s too hard."

    'a' will get its value from a form when I'm done testing so I won't know if or where in the string an apostrophe will be used.
    Last edited by Martin46766474; 04-09-2003 at 08:18 PM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    string.replace(/'/g,'\\\'');
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excellent, thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •