We can use your regular expression <#|#>
without problems. Thus, using the method split()
, as requested, the following can be done:
/* 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 a array (vector) with the values "delimited text. 1" and "delimited text. 2".
At the end of the code, there is a for
which serves to remove empty items from array created by the split. Explaining:
The split()
takes everything that "home" (match) and throws away and, what not "house", it returns as a array. However, how the split takes everything left and right of what "married" (but who was not married), where there is nothing he simply takes this "nothing" and puts as another item of the array resultant.
It is worth noting that the case of text not between "<#" and "#>" (in this order): the portion of text that is not among "<#" and "#>" is seen as bordering them (as explained above), even if it is not among the bounders themselves. This is because the ER used does not see these delimiters as a unit, but as two distinct separators because they are separated by "or" (|). Example:
change the code above with
var dados = new String("a<#texto.delimitado.1#>b<#texto.delimitado.2#>c");
the final result will be 5 items: "a", "text.delimited. 1", "b", "delimited text. 2" and "c"
Thus, it is important that, if this occurs, use an algorithm that removes first these unwanted text data. 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);
}
}
The novelty (algorithm added) has been marked in the code. Changes have been made to the variables name to conform to the new code.
What the added algorithm does is as follows: it searches 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 method match()
. The result would be a array. But what’s in the while
is precisely a way of uniting the entire result obtained as if it were a single string so that the algorithm (which already had itself) can separate everything with its ER.
That’s it; I hope I’ve helped!
+1 for simplicity in code and explanation.
– Maximiliano Guerra