从字符串 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
Remove string from a string jquery
提问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 replace
does not replace the characters in your original string but returns a new string with the replacement.
您的问题是replace
不会替换原始字符串中的字符,而是返回带有替换的新字符串。
myString = myString.replace(avoid,'');
回答by Jace
replace
does 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
回答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);