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

436
Views
How to add http:// if it doesn't exist in a string containing multiple URLs

String which I am checking:

   Unroll the pastry on to a lightly floured work surface. Cut is [Google](www.google.com/) 
    into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller reckk
    Bake in the oven for 25–30 minutes, or until the pastry has turned golden-brown and looks crisp. 
    Remove from the oven and leave to cool slightly before serving. Unroll the pastry on to a lightly floured work surface. 
    this is my [web](www.stackoverflow.com), this is [Google](https://www.google.com/) 
    Cut into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller rectangles into eight equal sections. You now have 16 rectangles in total.
    Brush one end of each rectangle with a little of the beaten egg

Above is the string from which I am checking the URL if the URL's doesn't have http or https I am adding it using below logic but problem here is it just check the first URL and replace it if http or https doesn't exist no other URL's are changed which don't have http or https

Below is the logic which I had applied:

  async urlify(text) {
   var urlRegex = new RegExp(
  '([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?'
   );
  return await text.replace(urlRegex, (url) => {
   if (url.search(/^http[s]?\:\/\//) === -1) {
    return 'http://' + url;
   } else {
     return url;
   }
  });
  }

Can anyone please let me know what's wrong here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use the g modifier. Without it, your regexp will only find the first match. Also, String.replace() doesn't return a Promise. You don't need await.

const str = ` Unroll the pastry on to a lightly floured work surface. Cut is [Google](www.google.com/) 
    into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller reckk
    Bake in the oven for 25–30 minutes, or until the pastry has turned golden-brown and looks crisp. 
    Remove from the oven and leave to cool slightly before serving. Unroll the pastry on to a lightly floured work surface. 
    this is my [web](www.stackoverflow.com), this is [Google](https://www.google.com/) 
    Cut into 2 long rectangles. Cut the large rectangle in half lengthways, then cut both smaller rectangles into eight equal sections. You now have 16 rectangles in total.
    Brush one end of each rectangle with a little of the beaten egg`;
    
function urlify(text) {
   var urlRegex = new RegExp(
    '([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?', 
    'g'
   );
  return text.replace(urlRegex, (url) => {
   if (url.search(/^http[s]?\:\/\//) === -1) {
    return 'http://' + url;
   } else {
     return url;
   }
  });
}
  
console.log(urlify(str));

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!