robue-a7119895
robue-a7119895

Reputation: 816

regular expression inside json

I would like to validate a url against a pattern. this is the URL

/search?name=john

Now, john could be any name, so it can be any sting. But, the/search?name= part of the URL should remain the same always.

I tried this regex and it did not work for me.

^([/search\\?name])=([a-z]+)$"

Upvotes: 1

Views: 469

Answers (1)

M A
M A

Reputation: 72844

Remove the square brackets [] around the string /search\\?name. The brackets are used to define character classes. An expression of the form [abc] would not match the string abc, but only either of the characters a, b or c. Hence the regex should be:

^(/search\\?name)=([a-z]+)$

There's also no need for parentheses unless you're capturing subpatterns in groups:

^/search\\?name=[a-z]+$

Upvotes: 1

Related Questions