Solved

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

Posted on 2006-07-21
2
450 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 28

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now