Reputation: 20668
I have the following string:
here "is 'a' \"string\" that" does contain a 'lot of "weird"' stuff "i" to 'find'
I want to extract is 'a' \"string\" that
, lot of "weird"
, i
and find
.
Any ideas? Thanks!
Upvotes: 3
Views: 83
Reputation: 89557
You can try with this pattern:
/"((?:[^"\\]+|\\"|\\)*)"|'((?:[^'\\]+|\\'|\\)*)'/g
Content inside double quote is in group 1, content inside single quote is in group 2.
Notice: this solution is not totally waterproof for escaped quotes because if \"
, that can detect the pattern above, is preceded by another \
, then the \\
is seen as a literal \
and the quote is no more escaped!
To avoid this trap, you can check cases when you have an odd number of backslashes replacing \\"
by (?:\\{2})*\\"
, then the first pattern will look like this:
/"((?:[^"\\]+|(?:\\{2})*\\"|\\)*)"|'((?:[^'\\]+|(?:\\{2})*\\'|\\)*)'/g
Upvotes: 2
Reputation: 785176
This negative lookbehind based regex should work for you:
/(["']).*?(?<!\\)\1/g
Regex without support of lookbehind e.g. Javascript
/(['"])(?:\1|.*?[^\\]\1)/g
Upvotes: 2