so I have a string like this:
var a = " 'asdf'+'asdf'+'we' + 1";
I am able to select the three quotes with:
'([^']*)'
How do I select the start of the single quote(') in the string using regex? And also a separate regex to select the end of the single quote(')? Is this possible with regex? I will replace the start of the quote with <<.
Final string will look like
" <<asdf'+<<asdf'+<<we' + 1"
When you need to replace some part of a pattern only, use capturing group round the part you need to keep, and just match (and consume) those parts you need to remove.
Here,
'- matches and consumes'([^']*')- matches and captures into Group 1 zero or more chars other than'(with[^']*) and then a'. The$1in the replacement pattern is a placeholder for the contents stored in Group 1 after a valid match is found.([^']*)- only captures any 0+ chars other than'into Group 1 so, the final'is matched and consumed, and will be missing in the$1placeholder content.Also, see the regex demo 1 and regex demo 2.