最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove unwanted characters from a string - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 10

Behold 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 "]"
发布评论

评论列表(0)

  1. 暂无评论