Skip to content
Advertisement

Fetch a string between a static pattern in HTML [closed]

script that will extract string between a particular pattern in HTML file.

Example: suppose if the HTML text has:

<p translate="index_word1" > </p>

Output should be: index_word1

Basically want a string between translate=”this string“.

Advertisement

Answer

You could use DOMParser to convert the string to a document, after which you could querySelectorAll over it to find the elements with translate attributes:

const str = `<p translate="index_word1" > </p>
<strong translate="index_word2"></strong>`;
new DOMParser()
  .parseFromString(str, 'text/html')
  .querySelectorAll('[translate]')
  .forEach(element => console.log(element.getAttribute('translate')));
Advertisement