Reputation: 125
I have a String for example this String:
bigString = "<div dir=\"rtl\" style=\"text-align: right\"><ul><li><span>רשאי, רק כאשר הרכב העוקף הוא רכב משא.</span></li><li><span>רשאי, רק בדרך שאינה עירונית.</span></li><li><span id=\"correctAnswer1759\">רשאי.</span></li><li><span>רשאי, אם באוטובוס אין נוסעים.</span></li></ul><div style=\"padding-top: 4px;\"><span><button type=\"button\" onclick=\"var correctAnswer=document.getElementById('correctAnswer1759');correctAnswer.style.background='yellow'\">הצג תשובה נכונה</button></span><br/><span style=\"float: left;\">| «D» | </span></div></div>"
I want to get each one of those Strings.
For that I decided to save in array the first String between <li><span>HERE</span></li>
and then remove the tags that was covering it from the big String and to do it 3 times until each one of those Strings are in array.
I could do it with Java but there is another problem with Java so I decided to do it in JavaScript.
In Java I could use this function to get the first String:
arr[j] = StringUtils.substringBetween(bigString,"<li><span>","</span></li>");
And then replace the first <li><span> </span></li>
tags with those functions:
bigString = bigString.replaceFirst("<li><span>","<null>");
bigString = bigString.replaceFirst("</span></li>","<null>");
Now I can't find proper functions to replace the Java ones.
var bigString = data.records[0].description4;
var end = "</span></li>";
var start = "<li><span>";
var strings = []; bigString.split(end).forEach(s => {index = s.indexOf(start);
if (index >= 0) {strings.push(s.substring(index + 3)); }});
for(var i = 0; i < 3; i++){
console.log(strings[i]);
}
Result: https://prnt.sc/w1wugv
The result I need: רשאי, רק כאשר הרכב העוקף הוא רכב משא. רשאי, רק בדרך שאינה עירונית. רשאי, אם באוטובוס אין נוסעים.
Upvotes: 3
Views: 164
Reputation: 45
I've got an Solution for you that is written in C#, but im sure you could translate it into JS 'cause it's not using any C# specific methods.
Take a look at my code:
int openTagStart = fulltext.IndexOf("<", pos);
int openTagEnd = fulltext.IndexOf(">", pos);
int closedTagStart;
int closedTagEnd;
int textStartPos;
string textInTags;
tag = fulltext.Substring(openTagStart + 1, openTagEnd - (openTagStart + 1));
textStartPos = openTagEnd + 1;
closedTagStart = fulltext.IndexOf("</" + tag + ">", textStartPos);
closedTagEnd = closedTagStart + 2 + tag.Length;
textInTags = fulltext.Substring(textStartPos, closedTagStart - (textStartPos));
You have to iterate it twice because of your nested Tags. But its pretty universal because you don't have to care about the kind of tags.
EDIT: Or you could try get an XML/HTML parser for JS, this might be the most elegant way to handle this.
Upvotes: 0
Reputation: 3589
This code encloses the desired elements with a unique string. Then it splits the text on this unique string and if the required tags are present it takes the text content
var bigString = '<div dir="rtl" style="text-align: right"><ul><li><span>רשאי, רק כאשר הרכב העוקף הוא רכב משא.</span></li><li><span>רשאי, רק בדרך שאינה עירונית.</span></li><li><span id="correctAnswer1759">רשאי.</span></li><li><span>רשאי, אם באוטובוס אין נוסעים.</span></li></ul><div style="padding-top: 4px;"><span><button type="button" onclick="var correctAnswer=document.getElementById("correctAnswer1759");correctAnswer.style.background="yellow"">הצג תשובה נכונה</button></span><br/><span style="float: left;">| «D» | </span></div></div>';
var res = [];
var splStr = bigString.replace(/<li><span>/gi, ',,#*&,,<li><span>')
.replace(/<\/span><\/li>/gi, '</span></li>,,#*&,,')
.split(",,#*&,,");
for (let i = 0; i < splStr.length; i++) {
if (splStr[i].indexOf('<li><span>') > -1 && splStr[i].indexOf('</span></li>') > -1) {
res.push(splStr[i].replace(/<\/?[^>]+(>|$)/g, ""));
}
}
console.log(res);
Upvotes: 1
Reputation: 2540
You can use regex to get result -
var bigString = '<div dir="rtl" style="text-align: right"><ul><li><span>רשאי, רק כאשר הרכב העוקף הוא רכב משא.</span></li><li><span>רשאי, רק בדרך שאינה עירונית.</span></li><li><span id="correctAnswer1759">רשאי.</span></li><li><span>רשאי, אם באוטובוס אין נוסעים.</span></li></ul><div style="padding-top: 4px;"><span><button type="button" onclick="var correctAnswer=document.getElementById("correctAnswer1759");correctAnswer.style.background="yellow"">הצג תשובה נכונה</button></span><br/><span style="float: left;">| «D» | </span></div></div>';
var matches = bigString.matchAll(new RegExp(/<li><span>(.*?)<\/span><\/li>/g));
var groups = Array.from(matches);
var result = groups.map((m) => m[1]);
console.log(result);
Upvotes: 0
Reputation: 391
It's definetly not the cleanest one, but I would propose a solution like this to fetch all strings into an array:
function getMiddleStrings(bigString, begin, end) {
strings = [];
bigString.split(end).forEach(s => {
index = s.indexOf(begin);
if (index >= 0) {
strings.push(s.substring(index + begin.length));
}
});
return strings;
}
The array strings
contains all the substrings then.
By calling getMiddleStrings(bigString, "<li><span>", "</span></li>")
you are then basically splitting the String at every </span></li>
tag and then removing all the stuff before and including all the <li><span>
tags.
Upvotes: 1