Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx - Match Character only when it's not proceeded or followed by same character

How would I match the quotations around "text" in the string below and not around "TEST TEXT" using RegEx. I wanted just quotations only when they are by themselves. I tried a negative lookahead (for a second quote) but it still captured the second of the two quotes around TEST TEXT.

This is some "text". This is also some ""TEST TEXT""

Be aware that I need this to scale so sometimes it would be right in the middle of a string so something like this:

/(\s|\w)(\")(?!")/g (using $2...)

Would work in this example but not if the string was:

This is some^"text".This is also some ""TEST TEXT""

I just need quotation marks by themselves.

EDIT

FYI, this needs to be Javascript RegEx so lookbehind would not be an option for me for this one.

like image 267
MillerMedia Avatar asked Oct 27 '25 12:10

MillerMedia


1 Answers

Since you have not tagged any particular flavor of regex I am takig liberty of using lookbehind also. You can use:

(?<!")"(?!")[^"]*"

RegEx Demo

Update: For working with Javascript you can use this regex:

/""[^"]*""|(")([^"]*)(")/

And use captured group # 1 for your text.

RegEx Demo

like image 82
anubhava Avatar answered Oct 30 '25 03:10

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!