I'd need to replace all apostrophes inside my strings, only the ones in the href=" " tag, apostrophe outside should remain.
I'm using node.js.
Here my string:
This is an example text. I'd love to fix this.
<a href="https://www.example.com/i'd-love-to-fly/"> I'd love to fly link </a>
Inside the text there could be more urls
<a href="https://www.example.com/i'd-rather/">I'd rather link</a>.
for example
<a href="https://www.example.com/i'd-love-to-fly/"> I'd love to fly link </a>
should be
<a href="https://www.example.com/id-love-to-fly/"> I'd love to fly link </a>
I'm trying to use regex
/https[^"]++/
it selects the whole URL but then I don't know how to select and replace only the apostrophe
This would do it assuming you only have a single single-quote inside the URL:
(href="[^"]+)'([^"]+")
https://regex101.com/r/6zRLSC/1
If you have multiple single-quotes then a while loop can address that issue.
var string = `This is an example text. I'd love to fix this.
<a href="https://www.example.com/i'd-love-to-fly-but-don't-like-heights/"> I'd love to fly but don't like heights link </a>
Inside the text there could be more urls
<a href="https://www.example.com/i'd-rather/">I'd rather link</a>.`;
// While we match the pattern that we're targeting, keep on replacing
while(string.match(/(href="[^"]+)'([^"]+")/))
{
string = string.replace(/(href="[^"]+)'([^"]+")/g, '$1$2')
}
console.log(string);