Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-07-21
2
Medium Priority
?
468 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Assisted Solution

by:dakyd
dakyd earned 500 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 500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

618 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