删除某个字符后的所有内容

时间:2022-05-29 09:27:42

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.

是否有一种方法可以删除某个字符后的所有内容,或者只选择该字符后的所有内容?我从href获取值,直到"?"它总是会有不同数量的字符。

Like this

像这样

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

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

我希望href是/Controller/Action,所以我想在“?”之后删除所有内容。

I'm using this now:

我现在用这个:

 $('.Delete').click(function (e) {
     e.preventDefault();

     var id = $(this).parents('tr:first').attr('id');                
     var url = $(this).attr('href');

     console.log(url);
 }

7 个解决方案

#1


313  

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

Sample here

样品在这里

Edit:

编辑:

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:

编辑2:

Updated code to account for no '?':

更新代码以解释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/

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

#2


189  

You can also use the split() function. This seems to be the easiest one that comes to my mind :).

还可以使用split()函数。这似乎是我想到的最简单的一个:)。

url.split('?')[0]

jsFiddle Demo

jsFiddle演示

One advantage is this method will work even if there is no ? in the string - it will return the whole string.

这种方法的一个优点是即使没有,它也能工作。在字符串中——它将返回整个字符串。

#3


13  

var href = "/Controller/Action?id=11112&value=4444";
href = href.replace(/\?.*/,'');
href ; //# => /Controller/Action

This will work if it finds a '?' and if it doesn't

如果它找到了a '?如果没有的话

#4


2  

var fromLeft = 8;
var s = '/Verify/Action?id=11112&value=4444';
s = s.substring(fromLeft , s.indexOf('/'));
alert(s);

The output will be /Verify/

输出将是/Verify/

#5


0  

If you also want to keep "?" and just remove everything after that particular character, you can do:

如果你还想保留“?”,然后在那个特定的字符后删除所有的字符,你可以:

var str = "/Controller/Action?id=11112&value=4444",
    stripped = str.substring(0, str.indexOf('?') + '?'.length);

// output: /Controller/Action?

#6


-1  

Worked for me:

为我工作:

      var first = regexLabelOut.replace(/,.*/g, "");

#7


-2  

It can easly be done using JavaScript for reference see link JS String

可以轻松地使用JavaScript进行引用查看链接JS字符串。

EDIT it can easly done as. ;)

编辑它可以很容易地做。,)

var url="/Controller/Action?id=11112&value=4444 ";
var parameter_Start_index=url.indexOf('?');
var action_URL = url.substring(0, parameter_Start_index);
alert('action_URL : '+action_URL);

#1


313  

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

Sample here

样品在这里

Edit:

编辑:

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:

编辑2:

Updated code to account for no '?':

更新代码以解释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/

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

#2


189  

You can also use the split() function. This seems to be the easiest one that comes to my mind :).

还可以使用split()函数。这似乎是我想到的最简单的一个:)。

url.split('?')[0]

jsFiddle Demo

jsFiddle演示

One advantage is this method will work even if there is no ? in the string - it will return the whole string.

这种方法的一个优点是即使没有,它也能工作。在字符串中——它将返回整个字符串。

#3


13  

var href = "/Controller/Action?id=11112&value=4444";
href = href.replace(/\?.*/,'');
href ; //# => /Controller/Action

This will work if it finds a '?' and if it doesn't

如果它找到了a '?如果没有的话

#4


2  

var fromLeft = 8;
var s = '/Verify/Action?id=11112&value=4444';
s = s.substring(fromLeft , s.indexOf('/'));
alert(s);

The output will be /Verify/

输出将是/Verify/

#5


0  

If you also want to keep "?" and just remove everything after that particular character, you can do:

如果你还想保留“?”,然后在那个特定的字符后删除所有的字符,你可以:

var str = "/Controller/Action?id=11112&value=4444",
    stripped = str.substring(0, str.indexOf('?') + '?'.length);

// output: /Controller/Action?

#6


-1  

Worked for me:

为我工作:

      var first = regexLabelOut.replace(/,.*/g, "");

#7


-2  

It can easly be done using JavaScript for reference see link JS String

可以轻松地使用JavaScript进行引用查看链接JS字符串。

EDIT it can easly done as. ;)

编辑它可以很容易地做。,)

var url="/Controller/Action?id=11112&value=4444 ";
var parameter_Start_index=url.indexOf('?');
var action_URL = url.substring(0, parameter_Start_index);
alert('action_URL : '+action_URL);