Since Javascript doesn't support lookbehinds, we have to use \B
which matches anything a word boundary doesn't match.
In this case, \B'
makes sure that '
isn't to the right of anything in \w
([a-zA-Z0-9_]
). Likewise, '\B
does a similar check to the left.
(?:(.*?)(?=\B'.*?'\B)(?:(\B'.*?'\B))|(.*?)$)
(regex demo)
Use a callback function and check to see if the length of captures 1 or 3 is > 0 and if it is, return an uppercase on the match
**The sample uses \U
and \L
just to uppercase and lowercase the related matches. Your callback need not ever effect $2
's case, so "Adam" can stay "Adam", etc.
Unrelated, but a note to anyone who might be trying to do this in reverse. it's much easier to the the REVERSE of this:
(\B'.+?'\B)
regex demo