简体   繁体   中英

Remove string from a string jquery

I am trying to replace within a string using 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,'');

How to do it?

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

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

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

replace does not modify the string, it returns a modified string. So do:

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

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

Try this

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

Demo

Also, there is another approach:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM