I have below description, which I am trying to replace the links in href props with signed s3 links on the backend without DOM
So, I need to first loop over all the matching s3 links and replace it with signed one.
The problem is It replaces other parts as well. It doesn't know when to end the matching.
Here is what I am trying to do,
function replaceNonSignedS3WithSignedLink(text) {
const urlRegex = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegex, function(url) {
return 'https://signed-s3-link.com';
})
// or alternatively
// return text.replace(urlRegex, '<a href="$1">$1</a>')
}
const desc = `<h3>Introduction Session</h3><p>Monday, March 3, 2021 11:00AM - 01:00PM</p>
Click <a href='https://example.s3.amazonaws.com/assets/bio.pdf'>here</a>`;
const result = replaceNonSignedS3WithSignedLink(desc);
console.log(result)
The result I get is,
"<h3>Introduction Session</h3><p>Monday, March 3, 2021 11:00AM - 01:00PM</p>
Click <a href='https://signed-s3-link.com"
I was wrong with the link matching regex, this seems to work for my case.
let i = 0;
function urlify(text) {
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(urlRegex, function(url) {
if (url.includes('s3')) return 'https://signed-s3-link.com' + ++i; // I can process individual link here
return url;
})
}
var text = `<h3>Introduction Session</h3><p>Monday, March 3, 2021 11:00AM - 01:00PM</p>
Click <a href='https://example.s3.amazonaws.com/assets/bio.pdf'>here</a> <a href = 'https://hello.com'>Hi</a>`;
var html = urlify(text);
console.log(html)