i'm trying to grab a string like product/bob which works, but product/my%20first%20product does not.
Here's my regex:
^product/([A-Za-z_\-\s\%20]+)$
A开发者_C百科ny ideas what I'm doing wrong?
What you have written as your regular expression contains a few things in the collection which may not do what you expect, depending on the regular expression engine being used. \s: this could match \ or s, not space or tab. \%20: this could match \ as well as %, 2 and 0. Anyway, you want to match %20 in order. This means that you should use a branch. And if you put the - at the start or end, you don't need to escape it.
^product/((?:[A-Za-z_ -]|%20)+)$
(The (?:...) is a non-capturing group which is more efficient than the capturing group (...).)
However, I think it quite possible that whatever it is that you're using turns the %20 into a space character, and so you may be able to just turn the \s into a space and get rid of the \%20:
^product/([A-Za-z _-]+)$
Also consider whether you should allow more characters - for myself, I would use ^product/(.*)$ or ^product/([^/]*)$ - and handle it further in your script or whatever it is. But it depends somewhat on what you're using it with, and as you haven't specified that, I can't help immediately.
try this~
^product/([A-Za-z_-]|%20)+$
加载中,请稍侯......
精彩评论