简体   繁体   English

正则表达式网址以某些字符断开

[英]Regex URL breaking with certain characters

My regex works when doing things like foo/bar and one with spaces/two with spaces/three with spaces/four with spaces etc, but it directs to the 404 page when using urlencode in PHP. 我的regex可以在执行foo/bar类的工作时使用, one with spaces/two with spaces/three with spaces/four with spaces等,但是在PHP中使用urlencode时它会指向404页面。

The following test shows that encoded strings don't work in the URL: http://regex101.com/r/jP0gW1 以下测试表明,编码的字符串在URL中不起作用: http : //regex101.com/r/jP0gW1

Anyone have any ideas? 有人有想法么? It also breaks when using the "+" character. 使用“ +”字符时,它也会中断。

That's because you were not handling the % in your regex. 那是因为您没有在正则表达式中处理% \\w only matches alphanumerical characters. \\w仅匹配字母数字字符。

Adding a % to your regex made it work: 在您的正则表达式中添加%即可使其正常工作:

^([\w\ ]+)(?:/([\w\ ]+))?(?:/([\w\% ]+))?(?:/([\w\ ]+))?(?:/([\w\ ]+))?(?:/([\w\ ]+))?/?$

DEMO 演示

You can simplify your regex if you want to: 如果需要,可以简化正则表达式:

/^([\w\s%]+\/).*$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM