Reputation: 120
I am struggling with regExp.
I just want to parse incoming urls to my application.
Here is my current RegExp :
var regexp = new RegExp('/users','i');
"/users".test(regexp); //Should return true
"/users/".test(regexp); //should return true
"/users?variable1=xxx&variable2=yyy&variableN=zzzz".test(regexp); //Should return true
"/users/?variable1=xxx&variable2=yyy&variableN=zzzz".test(regexp); //should return true;
"/users?variable1=xxx&variable2=yyy&variableN=zzzz/10".test(regexp); //Should return false
"/users/?variable1=xxx&variable2=yyy&variableN=zzzz/10".test(regexp); //should return false;
"/users/10".test(regexp); //should return false
"/users/10/contracts".test(regexp); //Should return false
"/users/10/contracts/10".test(regexp); //Should return false
"/users/anythingElseThatIsNotAQuestionMark".test(regexp); //Should return false
Is anyone has the kindness to help me ?
Wish you a nice evening.
Upvotes: 1
Views: 51
Reputation: 51876
/^\/users\/?(\?([^\/&=]+=[^\/&=]*&)*[^\/&=]+=[^\/&=]*)?$/i
should work and also confirm that the query is valid:
var regexp = /^\/users\/?(\?([^\/&=]+=[^\/&=]*&)*[^\/&=]+=[^\/&=]*)?$/i;
function test(str) {
document.write(str + ": ");
document.write(regexp.test(str));
document.write("<br/>");
}
test("/users"); //should return true
test("/users/"); //should return true
test("/users?variable1=xxx&variable2=yyy&variableN=zzzz"); //should return true
test("/users/?variable1=xxx&variable2=yyy&variableN=zzzz"); //should return true
test("/users?variable1=xxx&variable2=yyy&variableN=zzzz/10"); //should return false
test("/users/?variable1=xxx&variable2=yyy&variableN=zzzz/10"); //should return false
test("/users/10"); //should return false
test("/users/10/contracts"); //should return false
test("/users/10/contracts/10"); //should return false
test("/users/anythingElseThatIsNotAQuestionMark"); //should return false
Upvotes: 0
Reputation: 6476
First of all its RegExp.test(String)
Then this regex should do:
/^\/users\/?(?:\?[^\/]+)?$/i
Check it out: https://regex101.com/r/mK8dU4/1
Upvotes: 1