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

361
Views
How to get first URL contain in a string with JavaScript (Nodejs)?

In Node.js I have a string like below but i don't know how to get first URL contain inside that string:

"

<p> You left when I believed you would stay. You left my side when i needed you the most</p>**<img src="https://cloud-image.domain-name.com/storage/images/2021/picture_01.png" />** <br/> <p>Today, I don't have any bad words for you. I just want to thank you</p> **<img src="https://cloud-image.domain-name.com/storage/images/2021/picture_02.jpg" />

I want to get one result:

https://cloud-image.domain-name.com/storage/images/2021/picture_01.png
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is a bit of a workaround, however, you can scan the string you get

var matches = string.match(/\bhttps?:\/\/\S+/gi);

then just do matches[0], however a few things work around this so alternatively, you could use a library such as URL Knife if you want a robust method with a library.

https://github.com/Andrew-Kang-G/url-knife

about 4 years ago · Juan Pablo Isaza Report

0

The indexOf() method returns the index of the first occurrence. You can learn more about string methods on w3schools

const a = `<p> You left when I believed you would stay. You left my side when i needed you the most</p>**<img src="https://cloud-image.domain-name.com/storage/images/2021/picture_01.png" />** <br/> <p>Today, I don't have any bad words for you. I just want to thank you</p> **<img src="https://cloud-image.domain-name.com/storage/images/2021/picture_02.jpg" />`

const start=a.indexOf("https") //To get the index of the first link. (Returns 104)
const end=a.slice(start).indexOf('" />') //To get the end index of end link (Returns 70)
a.slice(start,start+end) //Returns 'https://cloud-image.domain-name.com/storage/images/2021/picture_01.png'

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!