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 5 of 5
  1. #1
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts

    Apostrophe in regular expression

    I have the following which strips out single and double
    quotes ' and " from the user input.

    function filter(str) {
    noquotes = /\'|"/g;
    return str.replace(noquotes, "");
    }

    This works fine but I am having trouble in modifying the script
    to retain single quotes (apostrophes) which do not fall on a word
    boundary.

    E.g. "Piper's Wood" or 'Piper's Wood' in quotes
    to be changed to Piper's Wood
    and not Pipers Wood

    function filter(str) {
    noquotes = /\'|"\b/g;
    return str.replace(noquotes, "");
    }

    does not work.

    Would you oblige me, Whammy?

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, who needs Whammy?

    return str.replace( /['"](?!\b)/g , "").replace( /([^\w])['"]/g, "$1" );
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Thank, Beetle.

    But your code does not remove the ' or " at the start of the string.

    Could you suggest a mod?

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Certainly

    return str.replace( /['"](?!\b)/g , "").replace( /([^\w])['"]/g, "$1" ).replace( /^['"]|['"]$/g, "" );
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    That's it Beetle! Thanks a million!


  •  

    Posting Permissions

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