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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5631384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:09:36  来源:igfitidea点击:

Remove everything after a certain character

javascriptjquery

提问by Dejan.S

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/Actiononly, so I want to remove everything after the "?".

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

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);
 }

回答by Demian Brecht

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

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

我还应该提到本机字符串函数比正则表达式快得多,正则表达式应该只在必要时使用(这不是这些情况之一)。

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

Sample here

样品在这里

回答by kapa

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.

一个优点是即使?字符串中没有,这种方法也能工作- 它会返回整个字符串。

回答by James Kyburz

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

如果找到“?”,这将起作用 如果没有

回答by patad

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

如果您还想保留“?” 并删除该特定字符之后的所有内容,您可以执行以下操作:

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

// output: /Controller/Action?

回答by Code Maniac

May be very late party :p

可能是很晚的派对:p

You can use a back reference $'

您可以使用反向引用$'

$' - Inserts the portion of the string that follows the matched substring.

let str = "/Controller/Action?id=11112&value=4444"

let output = str.replace(/\?.+/g,"$'")

console.log(output)

回答by Samina Samina

It works for me very nicely:

它非常适合我:

var x = '/Controller/Action?id=11112&value=4444';
var remove_after= x.indexOf('?');
var result =  x.substring(0, remove_after);
alert(x);

回答by Samina Samina

Worked for me:

为我工作:

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

回答by Imran

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

它可以很容易地使用 JavaScript 完成以供参考,请参阅链接 JS String

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