I have the following URLs:
我有以下网址:
www.localhost.com
localhost.com
test.localhost.com
How would I match "www" or nothing in a Regex?
如何在Regex中匹配“www”或“无”?
4 个解决方案
#1
20
(?:www)?
should match www or nothing.
应该匹配www或不匹配。
#2
0
/^www/
alows you to check if subjects starts with www
.
/ ^ www /向下检查如果受试者始于www。
#3
0
If I understand your question correctly, you need to use a lookahead, like this:
如果我正确地理解了你的问题,你需要使用一个前瞻,像这样:
/^www(?=\.localhost.com)/
This will match www
if and only if it's followed by .localhost.com
.
这将匹配www,如果且仅当它后面跟着。localhost.com。
If that's not what you're trying to do, please clarify.
如果这不是你想做的,请澄清。
#4
-2
www.+? It matches all string that has prefix as www
www。+ ?它匹配所有前缀为www的字符串。
#1
20
(?:www)?
should match www or nothing.
应该匹配www或不匹配。
#2
0
/^www/
alows you to check if subjects starts with www
.
/ ^ www /向下检查如果受试者始于www。
#3
0
If I understand your question correctly, you need to use a lookahead, like this:
如果我正确地理解了你的问题,你需要使用一个前瞻,像这样:
/^www(?=\.localhost.com)/
This will match www
if and only if it's followed by .localhost.com
.
这将匹配www,如果且仅当它后面跟着。localhost.com。
If that's not what you're trying to do, please clarify.
如果这不是你想做的,请澄清。
#4
-2
www.+? It matches all string that has prefix as www
www。+ ?它匹配所有前缀为www的字符串。