检查是否有效的蒸汽轮廓网址

时间:2016-05-04 00:27:33

标签: javascript jquery regex

我正在尝试制作一个正则表达式来检查用户发布的网址是否有效。

我创建了以下正则表达式:

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

但那不起作用。有没有一个正则表达式大师,这里可以告诉我我做错了什么?

我尝试验证的链接看起来像:

  

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

的answere

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

2 个答案:

答案 0 :(得分:1)

试试这个正则表达式:

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

参见此演示:

Regex Demo

“g”修饰符就是这样你可以看到它用不同的字符串测试它,但你可能不需要它。

答案 1 :(得分:1)

该问题的可接受答案也将与此网址匹配,例如:

https://steamcommunity.com/profile/thisisimpossible

当id由profile /而不是id /开头时,我正在使用以下正则表达式来防止非数字字符。不一定漂亮,但确实可以。

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