regex: remove all text within “double-quotes” (multiline included)

Try this expression:


Also make sure you replace globally (usually with a g flag – my PHP is rusty so check the docs).

Another edit: daalbert’s solution is best: a quote followed by one or more non-quotes ending with a quote.

I would make one slight modification if you’re parsing HTML: make it 0 or more non-quote characters…so the regex will be:


On second thought, here’s a better one:

This says: “a quote followed by either a non-whitespace character or white-space character any number of times, non-greedily, ending with a quote”

The one below uses capture groups when it isn’t necessary…and the use of a wildcard here isn’t explicit about showing that wildcard matches everything but the new-line char…so it’s more clear to say: “either a non-whitespace char or whitespace char” 🙂 — not that it makes any difference in the result.

there are many regexes that can solve your problem but here’s one:

this reads as:

find a quote optionally followed by: (any number of characters that are not new-line characters non-greedily, followed by any number of whitespace characters non-greedily), repeated any number of times non-greedily

greedy means it will go to the end of the string and try matching it. if it can’t find the match, it goes one from the end and tries to match, and so on. so non-greedy means it will find as little characters as possible to try matching the criteria.

great link on regex:
great link to test regexes:

Remember that your regex may have to change slightly based on what language you’re using to search using regex.

You can use single line mode (also know as dotall) and the dot will match even newlines (whatever they are):

You are using multiline mode which simply changes the meaning of ^ and $ from beginning/end of string to beginning/end of text. You don’t need it here.

Read more here: Source link