从字符串 jquery 中删除字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16010035/
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-26 16:10:45  来源:igfitidea点击:

Remove string from a string jquery

javascriptjquerystringreplace

提问by Okky

I am trying to replace within a string using jquery

我正在尝试使用 jquery 在字符串中替换

var myString ="qwerty"

var avoid ="t"

I want to do someting like

我想做点什么

myString.replace(avoid,'');

I was able to remove like myString.replace('t','');But i want it to be like myString.replace(avoid,'');

我能够删除喜欢myString.replace('t','');但我希望它像myString.replace(avoid,'');

How to do it?

怎么做?

JsFiddle : http://jsfiddle.net/nKSZT/

JsFiddle:http: //jsfiddle.net/nKSZT/

回答by halex

Your problem is that replacedoes not replace the characters in your original string but returns a new string with the replacement.

您的问题是replace不会替换原始字符串中的字符,而是返回带有替换的新字符串。

myString = myString.replace(avoid,'');

回答by Jace

replacedoes not modify the string, it returns a modifiedstring. So do:

replace不修改字符串,它返回修改后的字符串。所以这样做:

 var avoided = myString.replace(avoid,'');

Fiddle:
http://jsfiddle.net/MBjy3/1/

小提琴:http :
//jsfiddle.net/MBjy3/1/

回答by Amit

Try this

尝试这个

 var myString = "qwerty";
 alert(myString);
 var avoid = "t";
 var abc=myString.replace(avoid, '');
 alert(abc);

Demo

演示

回答by BlitZ

Also, there is another approach:

此外,还有另一种方法:

var myString ="qwerty",
    avoid = "t";

var result = myString.split(avoid).join('');

console.log(result);

回答by Patidar Praful

var str = "send_more_id4";
alert(str);
var res = str.replace("send_more_id", ""); 
alert(res);