Replace double quotes in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    Replace double quotes in a string

    Why doesn't this replace all double quotes in a string with the letter
    Q?

    source.replace( /"([^"]*)"/g, "Q$1Q" );

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Michael Winter

    #2
    Re: Replace double quotes in a string

    On Fri, 3 Dec 2004 17:44:09 +0000 (UTC), Christopher Benson-Manica
    <ataru@nospam.c yberspace.org> wrote:
    [color=blue]
    > Why doesn't this replace all double quotes in a string with the letter Q?
    >
    > source.replace( /"([^"]*)"/g, "Q$1Q" );[/color]

    It does for me. Are you forgetting to reassign the result to 'source'?

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Ivo

      #3
      Re: Replace double quotes in a string

      "Christophe r Benson-Manica" wrote[color=blue]
      > Why doesn't this replace all double quotes in a string with the
      > letter Q?
      >
      > source.replace( /"([^"]*)"/g, "Q$1Q" );[/color]

      Because your regex looks for strings with at least two quotes, possibly
      separated by other characters.
      To replace all double quotes, no matter what, the regex is at its simplest:

      source.replace( /"/g, 'Q' );

      HTH
      --
      Ivo



      Comment

      • Christopher Benson-Manica

        #4
        Re: Replace double quotes in a string

        Michael Winter <M.Winter@bluey onder.co.invali d> spoke thus:
        [color=blue]
        > It does for me. Are you forgetting to reassign the result to 'source'?[/color]

        *sheepish* Yes :(

        --
        Christopher Benson-Manica | I *should* know what I'm talking about - if I
        ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        Working...