How to select a text snippet in a String?

12

I'm creating a text file and using some delimiters ( <# and #> ) I need to select what's inside the <# texto.delimitado #> delimiter. Using the split() JavaScript function.

What would be the best regular expression for this? I already used Regular Expression ( <#|#> ), but it did not bring the desired result.

    
asked by anonymous 30.12.2013 / 04:43

4 answers

13

With this expression

/<#(.*?)#>/

You can capture text between <# and #> .

To get all match you need to use it as follows:

// cria um objeto RegExp com a flag global
var regex = new RegExp("<#(.*?)#>", "g");

var teste = "<# Meu primeiro teste aqui é # bem esperto #> "
            + "<# Este é meu # segundo # teste #>";

And to run the regex:

var match;
while ((match = regex.exec(teste))) // se chegou ao fim retorna null
{
    console.log(match[1]); // match[1] = o que está entre parenteses
}

Result:

Meu primeiro teste aqui é # bem esperto
Este é meu # segundo # teste 
    
30.12.2013 / 06:43
12

We can use your regular expression <#|#> without problems. Thus, using the split() method, as requested, you can do the following:

/* Declarações gerais */
var er = new RegExp("<#|#>","g");
var dados_arquivo = new String("<#texto.delimitado.1#><#texto.delimitado.2#>");
var i = new Number();
var resultado = new Array();

/* Obtém os dados que importam */
resultado = dados_arquivo.split(er);

/* Remove os itens não desejados (criados pelo método split) */
for(i = 0; i < resultado.length; i++)
{   
    if(resultado[i] == "")
    {
        resultado.splice(i,1);
    }
}

The result is an array (vector) with the values " texto.delimitado.1 " and " texto.delimitada .2 ".

At the end of the code , there is a for that serves to remove empty array items created by split. Explaining:

The split() takes everything that "matches" and throws away and, what does not "home", it returns as an array . But , as the split takes everything to the left and right of the "married" (but not married), where there is nothing, it simply takes this "nothing" in> array result.

It is worth noting that there is text that is not between " < # " and " # > ): The portion of text that is not between " & #;" and " # >" is viewed as their border (as explained above), even if it is not between the delimiters themselves. This is because the ER used does not see these delimiters as a unit, but rather as two distinct separators because they are separated by " or " (|). Example:

  • change the above code with

    var dados = new String("a<#texto.delimitado.1#>b<#texto.delimitado.2#>c");
    
  • The final result will be 5 items: "a", "texto.delimitado.1", "b", "texto.delimitado.2" and "c"

Thus , it is important that if this occurs, use an algorithm that removes these unwanted text from the data first. If this is the case, you can use the code below:

/* Declarações gerais */
var er = new RegExp("<#|#>","g");
var dados_arquivo = new String("a<#texto.delimitado.1#>b<#texto.delimitado.2#>c");
var i = new Number();
var resultado = new Array();

/* Algorítimo auxiliar // INÍCIO */
var er_auxiliar = new RegExp("<#.*?#>","g");
var texto_delimitado = dados_arquivo.match(er_auxiliar);

while(texto_delimitado.length > 1)
{   
    texto_delimitado[0] = texto_delimitado[0] + texto_delimitado[1];
    texto_delimitado.splice(1,1);
}
/* Algoritmo auxiliar // FIM */

/* Obtém dados que importam */
resultado = texto_delimitado[0].split(er); /* <- Foi trocada a variável */

/* Remove os itens não desejados (criados pelo método split) */
for(i = 0; i < resultado.length; i++)
{       
    if(resultado[i] === "")
    {
        resultado.splice(i,1);
    }
}

Novelty (algorithm added) has been tagged in the code. Changes were made to the variable name to conform to the new code.

What the added algorithm does is this: it looks in the data obtained from the original file (with the delimiters) and gets everything that is between " "and" # > "(by means of an auxiliary ER for the match() method.) The result would be an array it is just a way to unite all the result obtained as if it were a single string so that the algorithm (that already had) can separate everything with its ER.

That's it; I hope I have helped!

    
30.12.2013 / 10:17
2

Another way using filter and map :

var string = "bla bla <# texto.delimitado #> bla bla bla<# texto.delimitado2#>";
var resultado = string.split(/<#/).filter(function(v){
   return ~v.indexOf("#>");
}).map(function(v){
   return v.match(/(.*)#>/)[1].trim();
})
console.log(resultado);
    
25.06.2018 / 03:32
0

In a simpler way

var regex = /\[(.*?)\]/g;
var texto = '[Palavra chave 1 = 296] Se refere ao item do produto para direcionamento. [Palavra chave 2 = 1234]'
alert(texto.match(regex));
    
21.12.2016 / 20:12