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

179
Views
Regex para extraer todas las URL de imagen de la cadena HTML no es correcta

Estoy tratando de extraer todas las URL de imágenes de una cadena HTML usando regex /<img.*?src="(.*?)"[^>]+>/g en una función como esta:

 function getImages(string) { const imgRex = /<img.*?src="(.*?)"[^>]+>/g; const images = []; let img; while ((img = imgRex.exec(string))) { images.push(img[1]); } return images; }

Sin embargo, los resultados también contienen cosas que no son imágenes, por ejemplo:

 [ 'https://www.facebook.com/tr?id=900220307025564&ev=PageView&noscript=1', 'https://cyclingmagazine.ca/wp-content/uploads/2020/10/Peloton-Bike_Cam-1200x675.jpg', 'https://cyclingmagazine.ca/wp-content/uploads/2020/10/ontario-creates-logo.png', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"></div><div id=', 'https://cyclingmagazine.ca/wp-content/uploads/2019/03/cycling-pop-up.jpg' ]

que interrumpe la ejecución posterior. De ninguna manera soy un experto en expresiones regulares (claramente), agradecería cualquier ayuda.

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

0

No use expresiones regulares para esto. Usa la API DOMParser y su método parseFromString() .

 let str = "<img src='https://www.example.com'>"; let DOMParsing = new DOMParser() let parsed = DOMParsing.parseFromString(str, "text/html") // Now you can use querySelector to target the wanted element // or querySelectorAll and a loop for multiple elements let imgURL = parsed.querySelector("img").src console.log(imgURL)

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!