Javascript 检查document.location是否与特定模式匹配
比方说,如果document.location等于Javascript 检查document.location是否与特定模式匹配,javascript,regex,Javascript,Regex,比方说,如果document.location等于http://www.example.com或http://www.example.com/example/anythinghere/,但如果位置不准确,则为FALSE,例如http://www.example.com/example/anythinghere/sdjfdfasdfaf将返回FALSE 当然,我会写一些类似的东西: if(document.location == "http://www.example.com/" ||
http://www.example.com
或http://www.example.com/example/anythinghere/
,但如果位置不准确,则为FALSE,例如http://www.example.com/example/anythinghere/sdjfdfasdfaf
将返回FALSE
当然,我会写一些类似的东西:
if(document.location == "http://www.example.com/" ||
document.location == "http://www.example.com/example/*/")
然而,我知道好的ol'asterisk通配符不起作用,而且,作为正则表达式的业余爱好者,我无法找到合适的设置来查找与模式的精确匹配。对于条件语句的后半部分,您有什么建议?尝试只接受字母的正则表达式,如
[a-zA-Z]+
,否则http://www.example.com/example/;:ª*P=)#/”)#/
将有效
http://www.example.com/example/qwerty/uiop/
也将有效?以/
结尾,但有两个中间级别。尝试只接受字母的正则表达式,如[a-zA-Z]+
,否则http://www.example.com/example/;:ª*P=)35;/”)#/
将有效
http://www.example.com/example/qwerty/uiop/
也将有效吗?以/
结尾,但有两个中间级别。尝试只接受字母的正则表达式,如[a-zA-Z]+
,否则http://www.example.com/example/;:ª*P=)#/”)#/
将有效
http://www.example.com/example/qwerty/uiop/
也将有效?以/
结尾,但有两个中间级别。尝试只接受字母的正则表达式,如[a-zA-Z]+
,否则http://www.example.com/example/;:ª*P=)35;/”)#/
将有效
http://www.example.com/example/qwerty/uiop/
也将有效吗?以/
结尾,但有两个中间级别。您可以使用基于前瞻的正则表达式:
m = location.href.matches(/www\.example\.com\/(?=example\/)/i);
您可以使用基于前瞻的正则表达式:
m = location.href.matches(/www\.example\.com\/(?=example\/)/i);
您可以使用基于前瞻的正则表达式:
m = location.href.matches(/www\.example\.com\/(?=example\/)/i);
您可以使用基于前瞻的正则表达式:
m = location.href.matches(/www\.example\.com\/(?=example\/)/i);
下面的正则表达式应该可以做到这一点:
/^http:\/\/www\.example\.com\/(?:example\/[^\/]+\/)?$/
这是http://www.example.com/
,后跟可选的/example/somecharacters/
用法:
var re = /^http:\/\/www\.example\.com\/(?:example\/[^\/]+\/)?$/;
if(re.test(document.location.href) {
}
以下正则表达式应该可以执行此操作:
/^http:\/\/www\.example\.com\/(?:example\/[^\/]+\/)?$/
这是http://www.example.com/
,后跟可选的/example/somecharacters/
用法:
var re = /^http:\/\/www\.example\.com\/(?:example\/[^\/]+\/)?$/;
if(re.test(document.location.href) {
}
以下正则表达式应该可以执行此操作:
/^http:\/\/www\.example\.com\/(?:example\/[^\/]+\/)?$/
这是http://www.example.com/
,后跟可选的/example/somecharacters/
用法:
var re = /^http:\/\/www\.example\.com\/(?:example\/[^\/]+\/)?$/;
if(re.test(document.location.href) {
}
以下正则表达式应该可以执行此操作:
/^http:\/\/www\.example\.com\/(?:example\/[^\/]+\/)?$/
这是http://www.example.com/
,后跟可选的/example/somecharacters/
用法:
var re = /^http:\/\/www\.example\.com\/(?:example\/[^\/]+\/)?$/;
if(re.test(document.location.href) {
}
尝试使用以下方法进行匹配:
regex = /^http:\/\/www.example.com\/(?:example\/(?:[^\/]+\/)?)?$/
具体地说,这表示:
- 从字符串的实际开始处开始
- 匹配
- 作为可选组匹配(可选,因为培训?):
- 匹配示例/
- 作为可选的进一步组匹配:
- 至少包含1个字符的字符串,不包含正斜杠
- 后跟一个正斜杠
- 后跟字符串的末尾
regex.exec("http://www.example.com/example/anythinghere/") // matches
regex.exec("http://www.example.com/example/anythinghere") // doesn't match (no trailing slash)
regex.exec("http://www.example.com/example/anythinghere/qwe") // doesn't match (extra end chars)
regex.exec("http://www.example.com/exam") // doesn't match (no subdir)
regex.exec("http://www.example.com/") // matches
尝试使用以下选项进行匹配:
regex = /^http:\/\/www.example.com\/(?:example\/(?:[^\/]+\/)?)?$/
具体地说,这表示:
- 从字符串的实际开始处开始
- 匹配
- 作为可选组匹配(可选,因为培训?):
- 匹配示例/
- 作为可选的进一步组匹配:
- 至少包含1个字符的字符串,不包含正斜杠
- 后跟一个正斜杠
- 后跟字符串的末尾
regex.exec("http://www.example.com/example/anythinghere/") // matches
regex.exec("http://www.example.com/example/anythinghere") // doesn't match (no trailing slash)
regex.exec("http://www.example.com/example/anythinghere/qwe") // doesn't match (extra end chars)
regex.exec("http://www.example.com/exam") // doesn't match (no subdir)
regex.exec("http://www.example.com/") // matches
尝试使用以下选项进行匹配:
regex = /^http:\/\/www.example.com\/(?:example\/(?:[^\/]+\/)?)?$/
具体地说,这表示:
- 从字符串的实际开始处开始
- 匹配
- 作为可选组匹配(可选,因为培训?):
- 匹配示例/
- 作为可选的进一步组匹配:
- 至少包含1个字符的字符串,不包含正斜杠
- 后跟一个正斜杠
- 后跟字符串的末尾
regex.exec("http://www.example.com/example/anythinghere/") // matches
regex.exec("http://www.example.com/example/anythinghere") // doesn't match (no trailing slash)
regex.exec("http://www.example.com/example/anythinghere/qwe") // doesn't match (extra end chars)
regex.exec("http://www.example.com/exam") // doesn't match (no subdir)
regex.exec("http://www.example.com/") // matches
尝试使用以下选项进行匹配:
regex = /^http:\/\/www.example.com\/(?:example\/(?:[^\/]+\/)?)?$/
具体地说,这表示:
- 从字符串的实际开始处开始
- 匹配
- 作为可选组匹配(可选,因为培训?):
- 匹配示例/
- 作为可选的进一步组匹配:
- 至少包含1个字符的字符串,不包含正斜杠
- 后跟一个正斜杠
- 后跟字符串的末尾
regex.exec("http://www.example.com/example/anythinghere/") // matches
regex.exec("http://www.example.com/example/anythinghere") // doesn't match (no trailing slash)
regex.exec("http://www.example.com/example/anythinghere/qwe") // doesn't match (extra end chars)
regex.exec("http://www.example.com/exam") // doesn't match (no subdir)
regex.exec("http://www.example.com/") // matches
请尝试以下操作:
if(document.location == "http://www.example.com/" ||
/^http:\/\/www.example.com\/example\/[^\/]+\/?$/.test(document.location))
将测试您的URL是否匹配http://www.example.com/
完全正确,或者它是否使用正则表达式查看是否匹配http://www.example.com/example/ANYTHING_HERE_EXCEPT_FORWARD-SLASH/
尝试以下操作:
if(document.location == "http://www.example.com/" ||
/^http:\/\/www.example.com\/example\/[^\/]+\/?$/.test(document.location))
将测试您的URL是否匹配http://www.example.com/
完全正确,或者它是否使用正则表达式查看是否匹配http://www.example.com/example/ANYTHING_HERE_EXCEPT_FORWARD-SLASH/
尝试以下操作:
if(document.location == "http://www.example.com/" ||
/^http:\/\/www.example.com\/example\/[^\/]+\/?$/.test(document.location))
将测试您的URL是否匹配http://www.example.com/
完全正确,或者它是否使用正则表达式查看是否匹配http://www.example.com/example/ANYTHING_HERE_EXCEPT_FORWARD-SLASH/
尝试以下操作:
if(document.location == "http://www.example.com/" ||
/^http:\/\/www.example.com\/example\/[^\/]+\/?$/.test(document.location))
将测试您的URL是否匹配http://www.example.com/
完全正确,或者它是否使用正则表达式查看是否匹配http://www.example.com/example/ANYTHING_HERE_EXCEPT_FORWARD-SLASH/
出于某种原因,它不是