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!