#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    13
    Rep Power
    0

    Remove text between [" "]


    Hi

    I will have to process chunks of text which may contain ["text between square brackets and double quotes"]. The idea is to remove that text, including the quotes and brackets, and keeping the rest.

    My current regex is as follows:

    /\["[^"]*"\]/g

    This works fine on the following:

    Hello ["remove this"] world ["remove this too"] !

    Resulting in:

    Hello world !

    But it falls apart when I enter a quote inside [" "]:

    Hello ["remo"ve this"] world ["remove this too"] !

    In this case, the second [" "] is handles properly, but the first one stays put. What am I doing wrong?

    Thanks
    N6
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,962
    Rep Power
    9397
    Nothing. Your [^"] doesn't allow any quotes inside, so if there are any the match fails.

    Swap it for an ungreedy .*?
    Code:
    /\[".*?"\]/g
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    13
    Rep Power
    0
    That did the trick. Awesome!

    Thanks!!

IMN logo majestic logo threadwatch logo seochat tools logo