I am presented with this JavaScript array:
let a = [
[0, "<p><strong>Lorem Ipsum</strong> is simply dummy text of "],
[1, "<strong>"],
[0, "the"],
[1, "</strong>"],
[0, " printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type "],
[-1,"and"],
[1, "test"],
[0, " scrambled it to make a type"],
[1, " added"],
[0, "</p>"],
[1, "<ul><li>test</li></ul>"]
];
The task at hand involves extracting specific groups from the array based on certain conditions:
Consider a subarray from the array provided as an example:
[1, "<strong>"],
[0, "the"],
[1, "</strong>"]
This subsection qualifies as a group if a[0] == 1
and a[1]
represents the start of an HTML tag. Since a[1] contains <strong>
, indicating the beginning of a valid HTML tag, we aim to extract all elements between the start and end tag.
For instance, one such group could be structured like this:
let group = [
{
[1,"<strong>"],
[0,"the"],
[1,"</strong>"]
},
{
[1,"<ul><li>test</li></ul>"]
}
];
The goal is to identify groups based on the following criteria:
- If the first index of an element is 1 (i.e.,
a[i][0] == 1
) and ifa[i][1]
marks the beginning of a valid HTML tag. - If the first index of an element is 0 (i.e.,
a[i][0] == 0
) and it's preceded and succeeded by conditions outlined in Step 1 and Step 3. - If the first index of an element is 1 (i.e.,
a[i][0] == 1
) and ifa[i][1]
indicates the end of a valid HTML tag.
All these rules combined constitute a group or JavaScript object.
Another scenario to consider:
[1,"<ul><li>test</li></ul>"]
In this case, the array item encapsulates the complete group
<ul><li>test</li></ul>
, which should also be included in the resulting array.
Edit
I have refined my methodology
let a = [
[
0,
"<p><strong>Lorem Ipsum</strong> is simply dummy text of "
],
[
1,
"<strong>"
],
[
0,
"the"
],
[
1,
"</strong>"
],
[
0,
" printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type "
],
[-1,
"and"
],
[
1,
"test"
],
[
0,
" scrambled it to make a type"
],
[
1,
" added"
],
[
0,
"</p>"
],
[
1,
"<ul><li>test</li></ul>"
]
];
checkAndRemoveGroups(a, 1);
function checkAndRemoveGroups(arr, group) {
let htmlOpenRegex = /<([\w \d \s]+)([^<]+)([^<]+) *[^/?]>/g;
let groupArray = new Array();
let depth = 0;
//Iterate the array to find out groups and push the items
for (let i = 0; i < arr.length; i++) {
if (arr[i][0] == group && arr[i][1].match(htmlOpenRegex)) {
depth += 1;
groupArray.push({
Index: i,
Value: arr[i],
TagType: "Open"
});
}
}
console.log(groupArray);
}