我希望匹配网址直到特殊字符'_'(如果存在)
var url = window.location;
var url应该在'_'之前获取url的一部分
http://www.example.com/index.php?route=product/category&path=25_10
即 http://www.example.com/index.php?route=product/category&path=25
答案 0 :(得分:0)
split方法会这样做:
window.location.href.split('_')[0]
答案 1 :(得分:0)
var href = window.location.href;
// http://www.example.com/index.php?route=product/category&path=25_10
var new_url = href.slice(0, href.indexOf('_'));
// http://www.example.com/index.php?route=product/category&path=25