Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now