Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    use regex to find based on 2 characters but only replace one

    Help wanted · · · – – – · · ·
    2
    3
    45
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • aworkerb
      aworkerb last edited by

      I’d like to find all instances of a comma that are followed by a non-whitespace character, and then replace the comma with a line break but leave the non-whitespace character alone.

      It’s easy enough to find these matches using

      [,](\S)
      

      But I can’t figure out how to only replace the comma portion of the match… or even what the name of the approach/technique is so that I can read up on it. Any advice?

      (I worked around the problem for now by replacing comma followed by space with another symbol, then replacing all commas, then replacing the other symbol with comma + space again, but that’s a workaround I’d like to skip in the future.)

      Alan Kilborn 1 Reply Last reply Reply Quote 0
      • Alan Kilborn
        Alan Kilborn @aworkerb last edited by Alan Kilborn

        @aworkerb

        Very much like what was discussed HERE earlier today, so there are some links there to the docs that will help with your understanding.

        For you case I’d suggest [,](?=\S)

        Not sure why you made the comma a single thing inside the [ and ], but I didn’t change that part in my suggestion.

        1 Reply Last reply Reply Quote 3
        • aworkerb
          aworkerb last edited by

          lookbehind and lookahead assertions … aren’t part of the “real” match

          That’s the concept I wasn’t able to name.

          Not sure why you made the comma a single thing inside the [ and ] …

          Because I barely know what I’m doing and was piecing it together from possibly applicable Stack Exchange posts.

          Anyway, thanks, you nailed it!

          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          Copyright © 2014 NodeBB Forums | Contributors