如何检查网页网址是否包含'#'字符加上一些随机数字
e.g。 www.google.de/#1234
if( window.location.href.indexOf('#') > 0 ){
alert('true');
}
indexOf是否支持正则表达式?
答案 0 :(得分:8)
使用String.prototype.search
获取正则表达式的索引:
'https://example.com/#1234'.search(/#\d+$/); // 20
如果用于布尔检查,则RegExp.prototype.test
:
/#\d+$/.test('https://example.com/#1234'); // true
用于这些示例的正则表达式是/#\d+$/
,它将匹配文字#
,后跟字符串末尾的1位或更多位数。
正如评论中所指出的,您可能只想查看location.hash
:
/^#\d+$/.test(location.hash);
/^#\d+$/
将匹配包含1位或更多位数的哈希值。