删除某个角色后的所有内容 [英] Remove everything after a certain character

查看:91
本文介绍了删除某个角色后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法删除某个角色后的所有内容,或者只选择该角色的所有内容?我从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);

http: //jsfiddle.net/L4hna/1/

这篇关于删除某个角色后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆