How to search for words with javascript?

2

Hi. I need to get the responseTxt I got from a page and look for elements in it. The elements I need to display are those coming from a tag called "name name".

Example:

In the file there are some lines of type:

string(numero) = "nodo=""nome"

I need to find every place where "nodo=" is written and get every word followed by it. In case, if 3 occurrences of "nodo=" appear and each one has a word (ex: one has stone, another paper and other scissors), I have to get these 3 occurrences of nodo= and get the words of each one and print them on the screen.

    
asked by anonymous 24.03.2015 / 05:07

3 answers

1

Hi, you can find a word using the indexOf ('yourWord') method;

The method will return the index of the searched word, in case, I believe it will look like this:

var busca = "nodo=";
var indexBusca = seuRetorno.indexOf(busca);

To test if found, just check the value returned, in case, if it is different from -1, the word was found.

if (indexBusca != -1) {
    // seu código
}

Hugs.

    
24.03.2015 / 12:17
0

@LucianoZancan, based on your last example, I believe ResponseText is about an XML document serialized 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, we can then search for all tags nomes and retrieve propriedade name from them.

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 reply when the questioner did not give enough data, I will kill them because they are interesting.

If you want to work with plain text, you can search for the occurrence of the text "node name=" and then retrieve the next value that is enclosed in quotation marks.

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);

However, 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"));
    }
}
    
24.03.2015 / 12:27
0

You can use a regexp to do this:

Example:

In the middle of the text I put two expressions in the format that you indicated to exemplify:

  • [1]=> string(24) "<nodo name="background">" [1243]=> string(24) "<nodo name="xpto">"

  • The result will be: ["background","xpto"]

var text = 'array(173) { [0]=> string(10) "<document>" [1]=> string(24) "<nodo name="background">" [1243]=> string(24) "<nodo name="xpto">" [2]=> string(10) "<id>1</id>" [3]=> string(34) "<ocorrencia>0.3115942</ocorrencia>" [4]=> string(23) "<relacoes name="image">" [5]=> string(23) "<conexoes>48</conexoes>" [6]=> string(11) "</relacoes>" [7]=>';

var items = text.split(/nodo name="([^"]*)"/g);
var count = items.length;
var result = [];
for (var i = 1; i < count; i += 2)
    result.push(items[i]);

document.getElementById("output").innerHTML = JSON.stringify(result)
<div id="output"></div>

It's not the most performative form in the world, but it's the easiest way to do it.

    
24.03.2015 / 20:07