how to apply a between operation globally for a string

35 views Asked by At

I have an html string with multiple table tags.

   String str = '<table>......</table> <table>....</table>';

when I use string.js and the following code.

S(str).between('<table>', '</table>').s,

I am only getting the 1st table element of the string. I need to get all the table elements (possibly as an array). Is this possible with stringjs? is it better to use stringjs or a regular expression?

1

There are 1 answers

0
rishat On BEST ANSWER

I'd say you need to split the big string, remove occurrences of <table> and </table>, and then map through the resulting array. Something like

const str = '<table>......</table> <table>....</table>';
const subs = str.split('<table>').map(substring => substring.replace('</table>', ''));
console.log(subs);  // -> ['......', '....']

It only works when you have a huge string that represents many table siblings, and that's exactly what you asked for.