Reputation: 75
I have problem with JavaScript Regular Expression. I have test it on this website and it's working fine - http://www.pagecolumn.com/tool/regtest.htm .
var str = "Stalowa Wola;Nisko;Rzeszow";
var re = new RegExp("Stalowa Wola[a-zA-Z\W]*Rzeszow", "i");
var myArray = str.match(re);
console.log(myArray);
But when I trying to run this code on my website, it's not working.Console return 'null' and I don't know why. I noticed that when i remove 'Rzeszow' from RegExp it's starts working.
Upvotes: 0
Views: 896
Reputation: 20421
var str = "Stalowa Wola;Nisko;Rzeszow";
var re = new RegExp("Stalowa Wola[a-zA-Z\\W]*Rzeszow", "i");
var myArray = str.match(re);
console.log(myArray);
You need to escape the backslash.
Alternatively use this notation:
var str = "Stalowa Wola;Nisko;Rzeszow";
var re = /Stalowa Wola[a-zA-Z\W]*Rzeszow/i;
var myArray = str.match(re);
console.log(myArray);
The downside is that you must know the structure of the regex before runtime.
http://www.regular-expressions.info/javascript.html
Upvotes: 2