我需要从location.href中删除.com或任何域类型 例如:
bin/kafka-console-consumer.sh --new-consumer --topic test --from-beginning \
--bootstrap-server localhost:9092
我需要返回
sub.domain.com
谢谢!
答案 0 :(得分:0)
简单的方法:(不需要RegeEx)
var url = "sub.domain.com"
url = url.substring(0, url.lastIndexOf("."))
document.write(url)
答案 1 :(得分:0)
You can use lastIndexOf和substring
var str = 'sub.domain.com';
var end = str.lastIndexOf('.');
return str.substring(0, end);
完整的功能看起来很简单:
function stripTopLevelDomain(var domain) {
return domain.substring(0, domain.lastIndexOf('.'));
}
答案 2 :(得分:0)
"sub.domain.com".replace(/([.]\w+)$/, '')
[.] : the literal character .
\w+ : match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible
$ : assert position at end of the string
希望这有帮助。
alert("sub.domain.com".replace(/([.]\w+)$/, ''));
希望这有帮助。