简体   繁体   English

检查是否有效的蒸汽配置文件网址

[英]Check if valid steam profile url

I am trying to make a regex that checks if the url a user is posting, are valid.我正在尝试制作一个正则表达式来检查用户发布的 url 是否有效。

I have created the following regex:我创建了以下正则表达式:

/https?\\:\\/steamcommunity\\/.com\\/profiles|id\\/[a-zA-Z0-9]/

But that isn't working.但这不起作用。 Is there a regex master, here that can tell me what i am doing wrong?是否有正则表达式大师,可以告诉我我做错了什么?

The links i am trying to validate are looking like:我试图验证的链接看起来像:

https://steamcommunity.com/profiles/76561198009610232/ https://steamcommunity.com/id/rasmusvejby/ https://steamcommunity.com/profiles/76561198009610232/ https://steamcommunity.com/id/rasmusvejby/

ANSWERE回答

/(?:https?:\/\/)?steamcommunity\.com\/(?:profiles|id)\/[a-zA-Z0-9]+/

Try this regex:试试这个正则表达式:

(?:https?:\/\/)?steamcommunity\.com\/(?:profiles|id)\/[a-zA-Z0-9]+

See this demo:看这个演示:

Regex Demo正则表达式演示

The "g" modifier is just so you can see it test it in different strings, but you probably dont need it. “g”修饰符只是为了让您可以看到它在不同的字符串中测试它,但您可能不需要它。

The accepted answer to this question will also match this url for example:此问题的已接受答案也将匹配此 url,例如:

https://steamcommunity.com/profile/thisisimpossible

I am using the following regex to prevent non numeric characters when the id is prepended by profile/ rather than id/.当 id 由 profile/ 而不是 id/ 前缀时,我使用以下正则表达式来防止非数字字符。 Not necessarily prettier but it does the job.不一定更漂亮,但它可以完成工作。

^(?:https?:\/\/)?steamcommunity\.com\/(?:profiles\/[0-9]{17}|id\/[a-zA-Z0-9].*)$

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

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