删除某个角色后的所有内容 [英] Remove everything after a certain character
问题描述
有没有办法删除某个角色后的所有内容,或者只选择该角色的所有内容?我从href获得了值,直到?,它总是会有不同数量的字符。
Is there a way to remove everything after a certain character or just choose everything up to that character? I'm getting the value from an href and up to the "?", and it's always going to be a different amount of characters.
喜欢这个
/Controller/Action?id=11112&value=4444
我希望href仅为 / Controller / Action
,所以我想删除?之后的所有内容。
I want the href to be /Controller/Action
only, so I want to remove everything after the "?".
我现在正在使用它:
$('.Delete').click(function (e) {
e.preventDefault();
var id = $(this).parents('tr:first').attr('id');
var url = $(this).attr('href');
console.log(url);
}
推荐答案
var s = '/Controller/Action?id=11112&value=4444';
s = s.substring(0, s.indexOf('?'));
document.write(s);
编辑:
我还应该提到原生字符串函数比正则表达式快得多,正则表达式应该只在必要时使用(这不是其中一种情况) 。
I should also mention that native string functions are much faster than regular expressions, which should only really be used when necessary (this isn't one of those cases).
第2次编辑:
更新后的代码为没有? :
Updated code to account for no '?':
var s = '/Controller/Action';
var n = s.indexOf('?');
s = s.substring(0, n != -1 ? n : s.length);
document.write(s);
这篇关于删除某个角色后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!