1

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.

0

3 Answers 3

2

It's the other way around regexp.test("/users")

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.

Syntax

regexObj.test(str)

MDN

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help.
1

First of all its RegExp.test(String)

Then this regex should do:

/^\/users\/?(?:\?[^\/]+)?$/i

Check it out: https://regex101.com/r/mK8dU4/1

1 Comment

Thank you for your help. I really appreciate it.
0

/^\/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

1 Comment

Thank you for your help. I really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.