I have this url regex matching almost everything i need (port include), but excluding dash.
我有这个url regex匹配我需要的几乎所有东西(端口包括),但不包括破折号。
The regex:
正则表达式:
/^(http|https):\/\/\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\w]*)*$/
I want to include the possibility to have dash on it, but i don't achieve to do that. I tried to change .\w to -\w. but it does not seems to work.
我想要包含对它进行冲刺的可能性,但是我没有做到。我试着去改变。但这似乎行不通。
Does anyone know how to include dash on it ? Thanks.
有人知道怎么写吗?谢谢。
2 个解决方案
#1
2
You can change your pattern like that (I have removed all capture groups):
你可以这样改变你的模式(我已经删除了所有的捕获组):
/^https?:\/\/\w+(?:[.-]\w+)*(?::[0-9]+)?(?:\/\w+(?:[\/.-]\w+)*)?\/?$/
But keep in mind that the URL syntax can be more complicated.
但是请记住,URL语法可能更复杂。
#2
1
Dashes need to be escaped in a Regex : \-
, because they can appear in this syntax : [0-9]
需要在Regex: \-中转义破折号,因为它们可以在以下语法中出现:[0-9]
#1
2
You can change your pattern like that (I have removed all capture groups):
你可以这样改变你的模式(我已经删除了所有的捕获组):
/^https?:\/\/\w+(?:[.-]\w+)*(?::[0-9]+)?(?:\/\w+(?:[\/.-]\w+)*)?\/?$/
But keep in mind that the URL syntax can be more complicated.
但是请记住,URL语法可能更复杂。
#2
1
Dashes need to be escaped in a Regex : \-
, because they can appear in this syntax : [0-9]
需要在Regex: \-中转义破折号,因为它们可以在以下语法中出现:[0-9]