laravel 路由的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23328047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:25:55  来源:igfitidea点击:

Regular expression for laravel route

regexlaravellaravel-routing

提问by iOi

I want to make a regular expression for this laravel route.

我想为这个 Laravel 路线制作一个正则表达式。

Route::get('/{id}', 'HomeController@profile')

So far I was able to accept number only and string only using ->where('id', '\w+');and )->where('id', '\d+');

到目前为止,我只能使用->where('id', '\w+');和接受数字和字符串)->where('id', '\d+');

But, I would like to make the rule so it accepts strings from 0-9, hyphen -and any string from A-Zor a-z

但是,我想制定规则,以便它接受来自0-9,连字符的-字符串以及来自A-Z或的任何字符串a-z

How can I do this. I haven't done any regex before.

我怎样才能做到这一点。我以前没有做过任何正则表达式。

回答by Chirag Bhatia - chirag64

The regex you're looking for is: [0-9A-Za-z\-]+

您正在寻找的正则表达式是: [0-9A-Za-z\-]+