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

108
Views
Returning elements in array that appear after a certain word

I would like to return all elements in array that appear after a certain word

For example given the string

let testPattern = '<name>ladder</name><prx>112</prx><qty>12</qty><name>ladder</name><prx>200</prx>'

I would get the output

['ladder', 'prx: 112', 'qty: 12', 'ladder', 'prx: 200']

This is the code I have

const result = testPattern
    .match(/<[a-z]+>(.*?)<\/[a-z]+>/g)
    .map(val => val.replace(/<(\w+)\>(.*)\<\/\1\>/g, '$1: $2')
    .substring(testPattern.indexOf('ladder'),testPattern.length)
    )

This is what I get

[ 'ladder', '12', '2', 'ladder' ,'00']

Where am I going wrong

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

0

While you could do this with a regular expression, parsing the XML would make more sense.

let testPattern = '<name>ladder</name><prx>112</prx><qty>12</qty><name>ladder</name><prx>200</prx>';
const doc = new DOMParser().parseFromString(testPattern, 'text/html');
const arr = [...doc.body.children].map(
  child => child.nodeName === 'NAME' ? child.textContent : `${child.nodeName.toLowerCase()}: ${child.textContent}`
);
console.log(arr);

It's not actually HTML, but it still works.

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!