@Lucianozancan, based on his last example, I believe that Responsetext is a serialized XML document in an unconventional way.
In this case the interresante is to convert the Responsetext to an XML document and then work with the XML document:
Once we have the XML document, then we can search all tags nomes
and retrieve the propriedade name
of the same.
var responseText = '\
array(9) {\n\
[0]=> string(10) "<document>"\n\
[1]=> string(24) "<nodo name="background">"\n\
[2]=> string(10) "<id>1</id>"\n\
[3]=> string(34) "<ocorrencia>0.3115942</ocorrencia>"\n\
[4]=> string(23) "<relacoes name="image">"\n\
[5]=> string(23) "<conexoes>48</conexoes>"\n\
[6]=> string(11) "</relacoes>"\n\
[7]=> string(11) "</nodo>"\n\
[8]=> string(11) "</document>"\n\
}\
';
var conteudoText = responseText.split("\n");
var conteudoXML = [];
for (var indice in conteudoText) {
var text = conteudoText[indice];
var indice = text.indexOf('"');
if (indice >= 0) {
var xml = text.substring(indice + 1, text.length - 1);
conteudoXML.push(xml);
}
}
var documentXML = new DOMParser().parseFromString(conteudoXML.join("\n"),"text/xml");
var nodos = documentXML.getElementsByTagName("nodo");
for (var indice = 0; indice < nodos.length; indice++) {
var nodo = nodos[indice];
console.log(nodo.getAttribute("name"));
}
EDIT: The solutions below were attempts to answer when the author of the question had not given enough data, I will keep them interesting.
If you want to work with plain text, you can find the occurrence of the text "Node name=" and then retrieve the next value that is in quotes.
var responseText = '\
String (46)=> "name=""nome""/string" \
String (58)=> "nodo name=""documento""/html" \
String (35)=> "idade""40""qualquercoisa" \
String (43)=> "nodo name=""hoje""algoaqui" \
';
function encontrarValores(string, chave, separador, delimitador){
var indice = string.indexOf(chave, 0);
var valores = [];
do {
var iniIndice = indice + chave.length + separador.length + delimitador.length;
var fimIndice = string.indexOf(delimitador, iniIndice);
var valor = string.substring(iniIndice, fimIndice);
valores.push(valor);
indice = string.indexOf(chave, indice + 1);
} while (indice >= 0);
return valores;
}
var valores = encontrarValores(responseText, 'nodo name', '=', '""')
console.log(valores);
But if you were working with HTML, you could simply do the following:
var responseText = '\
<div nodo="externa">\
<div nodo="interna 1">Interna 1</div>\
<div nodo="interna 2">Interna 2</div>\
<div nodo="interna 3">Interna 3</div>\
</div">\
';
var container = document.createElement("div");
container.innerHTML = responseText;
var elements = container.querySelectorAll("[nodo]");
for (var indice in elements) {
var element = elements[indice];
if (element.nodeType == 1) {
console.log(element.getAttribute("nodo"));
}
}
Can you give a clearer example? these quotes are a little weird to have in a string
"nodo=""nome"
? joins the string you have pf.– Sergio
It’s a response from a post. I post via ajax on a website and get responseTxt. But this responseTxt is how the site sends. I wanted to filter. He sends everything like this, as in the example. But there are some things that this written string and something out of context and others (the ones that I care about) is written "nodo name="""Document" ( or other words). I hope that’s clearer. I just want to search for words in responseTxt preceded by a "node name=", because those are the ones I want to print on the screen. Hug!
– Luciano Zancan
I don’t have here now the full string of responseTxt but it’s like this (the model) ... String(46)=> "name=""name"""/string" string (58)=> "node name=""document""/html" string (35)=>"age""40"""anything" string (43)=> "node name=""today"""something". Note: This is a model of how it is, Nap remember what is written in the other "string". I need to take the following word to "node name="
– Luciano Zancan
Luciano, for each string "string(n) => ..." has a line break?
– Tobias Mesquita
Toby yes! There for 11 hrs~ 12 hrs I get home there put the answer given by the server on responseTxt. But each string (n) it prints as a line
– Luciano Zancan
Below is the server response (responseTxt), which I need to pick up only the words after Node name: Server response: array(173) { [0]=> string(10) "<Document>" [1]=> string(24) "<nodo name="background">" [2]=> string(10) "<id>1</id>" [3]=> string(34) "<occurrence>0.3115942</occurrence>" [4]=> string(23) "<relacoes name="image">" [5]=& ;string(23) "<connections>48</connections>" [6]=> string(11) "</relationships>" [7]=>
– Luciano Zancan
@Lucianozancan, could you update your question with Responsetext content? this type of information is not visible within a comment.
– Tobias Mesquita
@Lucianozancan, updated the answer, take a look at it to see if it solves your problem.
– Tobias Mesquita
Use a regular expression to search for patterns.
– Luan Fagundes