What regular expression can I use to replace text containing square brackets?

I have a body of text that I wish to perform text replacement on, using regular expressions.  What I cannot figure out is how to replace strings containing square brackets since they are not literal characters.

For example:

I have a body of text that looks like this:

This message is informative in nature. Your student [STU_LNAME], [STU_FNAME] ( [STU_EMAIL] ) has listed you as their company contact. .....  Your login username: [SUP_UNAME] and your password is: [SUP_PWORD] ...... Thank you."

I want to replace these strings:

var str = new Array("[STU_FNAME]","[STU_LNAME]","[STU_EMAIL]","[SUP_FNAME]","[SUP_LNAME]","[SUP_UNAME]","[SUP_PWORD]");

With these respective string values:

var rep = new Array("Jane","Smith","Jane.Smith@gmail.com","John","Doe","invisible_man","incognito");

I want to use a regular expression as so:

    for ( var i = 0; i < str.length; i++ ) {
            var str2rep = str[i];
            var re = new RegExp( str2rep, "g" );
            var msg = message.replace( re, rep[i] );
            message = msg;

But because the texts (keys) that i want to replace contain square brackets, this is not done correctly.

If you could, please give me either the loop code, or even just help on the correct regular expression to use.

Any feedback is much appreciated.  Thanks in advance.

-- Jason
Who is Participating?
Pravin AsarPrincipal Systems EngineerCommented:
A simpler solution would be not have  [ ] for array.
While genrating regular expression , use of \ will trun off the special meaning of [

The following code has been tested.

var rep = new Array("Jane","Smith","Jane.Smith@gmail.com","John","Doe","invisible_man","incognito");
var message = "Dear [SUP_FNAME] [SUP_LNAME]," +
              "This message is informative in nature. Your student [STU_LNAME], [STU_FNAME] ( [STU_EMAIL] ) " +
              "has listed you as their company contact. .....  Your login username: [SUP_UNAME] and your password is: "+
              "[SUP_PWORD] ...... Thank you. [SUP_FNAME] [SUP_LNAME]";
document.write ("<br>" + message);
for (ix=0; ix < str.length; ix++) {
      var regpat =  new RegExp("\\[" + str[ix] + "\\]"  , "g" );
      var tmp = message.replace(regpat, rep[ix]);
    message = tmp;
document.write ("<h1>"+ message + "</h1>");

You need to escape the square brackets with a "\" in your regular expression.  However, since you're creating these with strings, you need to make sure that the strings have the "\" character in them by using two of them in a row.  So, change your str definition to this:

var str = new Array("\\[STU_FNAME\\]","\\[STU_LNAME\\]","\\[STU_EMAIL\\]","\\[SUP_FNAME\\]","\\[SUP_LNAME\\]","\\[SUP_UNAME\\]","\\[SUP_PWORD\\]");

That should fix your regular expressions & do the replace() properly.  Hope that helps.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.