admin管理员组文章数量:1356595
I want to remove the characters [
and ]
inside a variable. How can I do this? My variable is something similar to this one:
var str = "[this][is][a][string]";
Any suggestion is very much appreciated!
I want to remove the characters [
and ]
inside a variable. How can I do this? My variable is something similar to this one:
var str = "[this][is][a][string]";
Any suggestion is very much appreciated!
Share Improve this question edited Nov 8, 2011 at 18:18 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Nov 8, 2011 at 18:16 unknownunknown 3853 gold badges6 silver badges18 bronze badges 1- possible duplicate of Remove characters from a string – Felix Kling Commented Nov 8, 2011 at 18:19
2 Answers
Reset to default 10Behold the power of regular expressions:
str = str.replace(/[\]\[]/g,'');
the fastest way
function replaceAll(string, token, newtoken) {
while (string.indexOf(token) != -1) {
string = string.replace(token, newtoken);
}
return string;
}
All you need to do is this...
var str = "[this][is][a][string]";
srt=replaceAll(str,'[',''); //remove "["
str=replaceAll(str,']',''); //remove "]"
本文标签: javascriptRemove unwanted characters from a stringStack Overflow
版权声明:本文标题:javascript - Remove unwanted characters from a string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744047924a2581878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论