Solved

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

Posted on 2006-07-21
2
455 Views
Last Modified: 2008-01-09
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:

"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."

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
0
Comment
Question by:propractice
2 Comments
 
LVL 19

Assisted Solution

by:dakyd
dakyd earned 125 total points
ID: 17156096
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.
0
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 125 total points
ID: 17156345
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 str = new Array("STU_FNAME","STU_LNAME","STU_EMAIL","SUP_FNAME","SUP_LNAME","SUP_UNAME","SUP_PWORD");
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>");

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question