Dejan.S Dejan.S - 1 year ago 132
Javascript Question

Remove everything after a certain character

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.

like /Controller/Action?id=11112&value=4444

I want the href to be
only, so I want to remove everything after the "?".

I'm using this now:

$('.Delete').click(function (e) {
var id = $(this).parents('tr:first').attr('id');
var url = $(this).attr('href');

Answer Source
var s = '/Controller/Action?id=11112&value=4444';
s = s.substring(0, s.indexOf('?'));

Sample here


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).

2nd Edit:

Updated code to account for no '?':

var s = '/Controller/Action';
var n = s.indexOf('?');
s = s.substring(0, n != -1 ? n : s.length);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download