有没有一种方法可以删除某个字符之后的所有内容,或者只选择该字符之前的所有内容?我从href中获取值,直到"?",并且它总是会有不同数量的字符。
像这样
/Controller/Action?id=11112&value=4444
我希望href仅为/Controller/Action
,所以我想删除"?“后面的所有内容。
我现在使用的是:
$('.Delete').click(function (e) {
e.preventDefault();
var id = $(this).parents('tr:first').attr('id');
var url = $(this).attr('href');
console.log(url);
}
发布于 2011-04-12 15:02:46
发布于 2011-04-12 14:52:47
var s = '/Controller/Action?id=11112&value=4444';
s = s.substring(0, s.indexOf('?'));
document.write(s);
我还应该提到,原生字符串函数比正则表达式快得多,正则表达式应该只在必要时使用(这不是其中的一种情况)。
更新代码以说明没有'?':
var s = '/Controller/Action';
var n = s.indexOf('?');
s = s.substring(0, n != -1 ? n : s.length);
document.write(s);
发布于 2011-04-12 14:53:52
var href = "/Controller/Action?id=11112&value=4444";
href = href.replace(/\?.*/,'');
href ; //# => /Controller/Action
如果它找到一个'?‘,它就会工作。如果它不能
https://stackoverflow.com/questions/5631384
复制相似问题