I am trying to write a regex for a URL match and it’s not working.
The subject URL is…
var subject = "http://encore.lsbu.ac.uk/iii/encore/search/C%257CSab%257COrightresult%257CU1?lang=eng&suite=pearl";
The regex is…
var regex = /http:\/\/encore.lsbu.ac.uk\/iii\/encore\/search\/C%257CS[a-z][A-Z][0-9]%257COrightresult%257CU1?lang=eng&suite=pearl/i;
And I am using JavaScript to test it.
var answer = regex.test(subject);
// answer is false
The goal is to match any URL with the regex which has keyword changed in the middle of the string but whole string matches the URL. In other words, both string should be matched except the part of keyword shouldn’t be checked. Am I doing anything wrong?
Here is the fixed regex:
/http:\/\/encore\.lsbu\.ac\.uk\/iii\/encore\/search\/C%257CS[a-z0-9]*%257COrightresult%257CU1\?lang=eng&suite=pearl/iI made the following changes to it:
[a-z][A-Z][0-9]to[a-z0-9]*to match any of those characters repeated zero or more times.+instead of*to match the characters once or more, or{2}to match them exactly twice (this is the case in your example string, but may not be in all cases).Edit: Removed the
A-Zsince it isn’t needed (the regex is case insensitive).