[英]Need a Javascript regex expression to match a language param in URL
I'm not familiar with regex and I haven't found any proper solution for my case. 我对正则表达式不熟悉,也没有找到适合我的情况的解决方案。 My website utilizes such URLs:
我的网站使用了以下URL:
(http(s)://www.)website.com/en/someURL
(http(s)://www.)website.com/anotherURL
(http(s)://www.)website.com/de/anotherURL
If website.com/someURL
, website.com/
or website.com
is accessed the default language will be loaded. 如果
website.com/someURL
, website.com/
或website.com
访问默认语言将被载入。
I need a regex which will allow only these certain language codes, case sensitivity is not important: 我需要一个仅允许这些特定语言代码的正则表达式,区分大小写并不重要:
(http(s)://www.)website.com/en
(http(s)://www.)website.com/en/
(http(s)://www.)website.com/
(http(s)://www.)website.com
(http(s)://www.)website.com/de
(http(s)://www.)website.com/de/
This regex will work: 此正则表达式将起作用:
^((https?)?\:\/\/www\.)?website\.com(\/((de|en)\/?)?)?$
If you want to make some unit tests on [ https://regex101.com/][1] you should erase the ^ and $ for beginning and end, otherwise you will see a positive result only if you use only one line. 如果要在[ https://regex101.com/][1]上进行一些单元测试,则应删除^和$作为开始和结尾,否则仅在只使用一行的情况下,您才能看到肯定的结果。
For testing you could use this (if you want you can add some lines): 为了进行测试,您可以使用它(如果需要,可以添加一些行):
https://www.website.com/en
http://www.website.com/en/
https://www.website.com/
https://www.website.com
https://www.website.com/de
https://www.website.com/de/
you only have to limit the number of letters to two and with that you get the language \\w{2}
您只需要将字母的数量限制为两个即可得到
\\w{2}
语言
https?\:\/\/www\.website\.com\/((\w{2})\/?$|(\w{2})\/)
eg https://regex101.com/r/SCbDyY/2 例如https://regex101.com/r/SCbDyY/2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.