I have this url:
http://example.com/categories/listing/all-categories?src=home
And I was testing online for regex here and ended up to this:
[^/]*(?=\?)
I want the part between asterisk http://example.com/categories/listing/*all-categories*?src=home
In my javascript I wrote this:
var reg = /[^\/]*(?=\?)/i;
var url = document.location.href;
var subcat = reg.exec(url);
alert(subcat);
But nothing alerts, what I doing wrong?
document.location.href. What doesalert(url)give?/inside the character class, and the case insensitive flag does not do anything here either. This will not fix your problem, just a note. So it can be simplified to:/[^/]*(?=\?)//must always be escaped within a JavaScript regex literal. Otherwise it would be interpreted as a regex delimiter, causing a Syntax Error. The/iis indeed superfluous.^,`,]` and-should be escaped. Did you try the regex I posted, no syntax error. More on it here: regular-expressions.info/charclass.html