Is it Possible to Concatenate variable in Split Regexp in Javascript? Eg: var.split(/[A-Z]/+ variable + / \d/); [duplicate]
Regexp Javascript , is it possible to concatenate ?
Ex 1: (Working Example)
var reg = new RegExp(/(\d+): ([A-Za-z0-9çÇáéíóúãõÁÉÍÓÚÃÕ]+): ([A-Za-z0-9çÇáéíóúãõÁÉÍÓÚÃÕ]+)/);
var arr = all_titles.split(reg);
I'm trying to do it but I can't figure it out how to concatenate, cause I actually want to concatenate a variable in it, but if I add the Quotes it just doesn't work.
Ex 2:
var reg = new RegExp("/(\d+): ([A-Za-z0-9çÇáéíóúãõÁÉÍÓÚÃÕ]+): ([A-Za-z0-9çÇáéíóúãõÁÉÍÓÚÃÕ]+)/");
var arr = all_titles.split(reg);
The Example 2 doesn't work for some reason (without even any concatenation of variable), then I stripped the delimiters and still didn't work.
What I want to do is get something like this -> 20: lalalalala: whateverIsWritten
var variable = "lalalalala";
var开发者_如何学Go reg = new RegExp("/(\d+): "+variable+": ([A-Za-z0-9çÇáéíóúãõÁÉÍÓÚÃÕ]+)/");
var arr = all_titles.split(reg);
Thanks in advance!
Try this:
new RegExp("(\\d+): "+variable+": ([A-Za-z0-9çÇáéíóúãõÁÉÍÓÚÃÕ]+)");
\
escaped, /
removed from start and end.
You can compile a regular expression from a variable, e.g.:
var regex = new RegExp();
var src = "\\s"; // don't forget to escape slashes!
var mods = "g";
regex.compile(src, mods);
alert(regex.source);
精彩评论