Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Replace links with some other link in a text

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,

  1. Collect all the links
  2. Filter links which has s3 related keyword
  3. replace them with signed s3 link

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"
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!