Skip to content

Instantly share code, notes, and snippets.

@X-Raym
Created November 7, 2024 10:21
Show Gist options
  • Save X-Raym/e46941c903c57a960d04201320ade7cd to your computer and use it in GitHub Desktop.
Save X-Raym/e46941c903c57a960d04201320ade7cd to your computer and use it in GitHub Desktop.
Javascript match all substring instances via regex and return array
const htmlString = "Normal normal <u>souligné</u> normal normal <u>souligné</u>";
const regex = /<u>(.+?)<\/u>/g;
let matches;
const results = [];
while ((matches = regex.exec(htmlString)) !== null) {
results.push({
match: matches[1],
index: matches.index
});
}
console.log(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment