regex - Javascript search and replace sequence of characters that contain square brackets - Stack Overflow

I'm trying to search for '[EN]' in the string 'Nationality [EN] [ESP]', I want

I'm trying to search for '[EN]' in the string 'Nationality [EN] [ESP]', I want to remove this from the string so I'm using a replace method, code examaple below

var str = 'Nationality [EN] [ESP]';
var find = "[EN]";
var regex = new RegExp(find, "g");
alert(str.replace(regex, ''));

Since [EN] is identified as a character set this will output the string 'Nationality [] [ESP]' but I want to remove the square brackets aswell. I thought that I could escape them using \ but it didn't work

Any advice would be much appreciated

I'm trying to search for '[EN]' in the string 'Nationality [EN] [ESP]', I want to remove this from the string so I'm using a replace method, code examaple below

var str = 'Nationality [EN] [ESP]';
var find = "[EN]";
var regex = new RegExp(find, "g");
alert(str.replace(regex, ''));

Since [EN] is identified as a character set this will output the string 'Nationality [] [ESP]' but I want to remove the square brackets aswell. I thought that I could escape them using \ but it didn't work

Any advice would be much appreciated

Share Improve this question asked Apr 26, 2010 at 13:20 RuthRuth 5,82612 gold badges39 silver badges45 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Try setting your regex this way:

var regex = /\[EN\]/g;

If you just want to replace a single instance of it you can just str = str.replace("[EN] ", ""); otherwise; var find = "\\[EN\\]";.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745192158a4615914.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信