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

151
Views
How can I isolate substrings wrapped in markup tags?

Can anyone suggest a simple* way to do the following?

"this is <mark>just an</mark> example <mark>snippet</mark>"

to

["this is", "<mark>just an</mark>",  "example", "<mark>snippet</mark>" ]

Thanks for the answer guys

this snippet below covers tags with attributes also

"<b class="highlight">Paradise</b> Lost"
.split(/(<\w+\s+(?!term).*?>.*?().*?<\/[a-zA-Z]*>)/g)
.filter((i) => i)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Split using regex, but considering all HTML tags and web components with attributes, not just <mark>.

function splitHTML (inputString) {
  return inputString
    .split(/(<[a-zA-Z-](?!term).*?>.*?().*?<\/[a-zA-Z-]*>)/g)
    .filter((i) => i);
}

console.log(splitHTML('this is <mark>just an</mark> example <mark>snippet</mark>'));

The code above will work for:

  • <mark>text</mark>
  • <my-tooltip>web component</my-tooptip>
  • <mark class="red">colored text</mark>
about 4 years ago · Juan Pablo Isaza Report

0

Just split by regex, that will give you some empty elements.. you can filter empty elements afterwards.

let a = "this is <mark>just an</mark> example <mark>snippet</mark>";
let x = a.split(/( <mark>.*?().*?<\/mark>)/g); // ['one', '.two', '.three'];
console.log(x.filter( (el) =>el) );

about 4 years ago · Juan Pablo Isaza Report

0

function splitHTML (inputString) {
  const result = [];
  
  // 1. Replace a HTML tag with ###<mark> and </mark>###
  inputString = inputString.replaceAll('<mark', '###<mark');
  inputString = inputString.replaceAll('</mark>', '</mark>###');
  
  // 2. Split on the newly added sign
  inputString = inputString.split('###');
  
  // 3. Filter out empty lines and return the result
  return inputString.filter((a) => a);
}

console.log(splitHTML('this is <mark>just an</mark> example <mark>snippet</mark>')); // => ['this is ', '<mark>just an</mark>', ' example ', '<mark>snippet</mark>']

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!