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

像这样

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

我想href是/Controller/Action,所以我想删除“?”之后的所有东西。

我现在用这个:

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

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

     console.log(url);
 }

当前回答

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

url.split('?')[0]

jsFiddle演示

这种方法的一个优点是即使没有?在字符串中-它会返回整个字符串。

其他回答

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

url.split('?')[0]

jsFiddle演示

这种方法的一个优点是即使没有?在字符串中-它会返回整个字符串。

对我有用:

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

它可以很容易地完成使用JavaScript参考参见链接 JS字符串

编辑 这很容易做到。;)

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

你也可以使用split()方法,对我来说,这是实现这个目标最简单的方法。

例如:

让dummyString ="Hello Javascript:这是假字符串" [0] . dummyString = dummyString.split(':' console.log (dummyString) //返回"Hello Javascript" 来源:https://thispointer.com/javascript-remove-everything-after-a-certain-character/

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

如果找到'?’如果没有的话