Javascript search and replace sequence of characters that contain square brackets

Posted by Ruth on Stack Overflow See other posts from Stack Overflow or by Ruth
Published on 2010-04-26T13:20:25Z Indexed on 2010/04/26 13:23 UTC
Read the original article Hit count: 175

Hello all

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

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex