?
Solved

use variable in regexp

Posted on 2008-06-19
7
Medium Priority
?
311 Views
Last Modified: 2008-06-20
So I have a working regular expression that does what I want.  The problem is that I need to insert a variable into the regexp.  The code is attached.  How do I get the second code snippet to work.
WORKS
<script type="text/javascript">
  var re = /(\b)(glossary term)(?![^<]*>|[^<]*<\/a>)/g;
  var sourcestring = 'aglossary term <a href="page.aspx">glossary term</a> glossary term <test glossary term test> glossary term';
  var replacementpattern = '$1<a href="glossary.aspx#$2" title="def" class="glossary">$2</a>';
  var result = sourcestring.replace(re, replacementpattern);
  alert("result = " + result);
</script>
 
DOES NOT WORK
<script type="text/javascript">
  var word="glossary term";
  var re = new RegExp("(\b)("+word+")(?![^<]*>|[^<]*<\/a>)","g");
  var sourcestring = 'aglossary term <a href="page.aspx">glossary term</a> glossary term <test glossary term test> glossary term';
  var replacementpattern = '$1<a href="glossary.aspx#$2" title="def" class="glossary">$2</a>';
  var result = sourcestring.replace(re, replacementpattern);
  alert("result = " + result);
</script>

Open in new window

0
Comment
Question by:mattjp88
[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
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21826148
Try:
var re = new RegExp("(\\b)("+word+")(?![^<]*>|[^<]*<\\/a>)","g");

Open in new window

0
 
LVL 6

Author Comment

by:mattjp88
ID: 21826173
no dice
0
 
LVL 82

Expert Comment

by:hielo
ID: 21826348
What part is not working. Both seem to be alerting the same value
0
Industry Leaders: 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!

 
LVL 6

Author Comment

by:mattjp88
ID: 21826411
in FF 3 it alerts 2 different values
<script type="text/javascript">
  var re = /(\b)(glossary term)(?![^<]*>|[^<]*<\/a>)/g;
  var sourcestring = 'aglossary term <a href="page.aspx">glossary term</a> glossary term <test glossary term test> glossary term';
  var replacementpattern = '$1<a href="glossary.aspx#$2" title="def" class="glossary">$2</a>';
  var result = sourcestring.replace(re, replacementpattern);
  alert("result = " + result);
</script>
 
<script type="text/javascript">
  var word="glossary term";
  var re = new RegExp("(\b)("+word+")(?![^<]*>|[^<]*<\/a>)","g");
  var sourcestring = 'aglossary term <a href="page.aspx">glossary term</a> glossary term <test glossary term test> glossary term';
  var replacementpattern = '$1<a href="glossary.aspx#$2" title="def" class="glossary">$2</a>';
  var result = sourcestring.replace(re, replacementpattern);
  alert("result = " + result);
</script>

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 21826822
This is what I tried:
<script type="text/javascript">
  var re = /(\b)(glossary term)(?![^<]*>|[^<]*<\/a>)/g;
  var sourcestring = 'aglossary term <a href="page.aspx">glossary term</a> glossary term <test glossary term test> glossary term';
  var replacementpattern = '$1<a href="glossary.aspx#$2" title="def" class="glossary">$2</a>';
  var result = sourcestring.replace(re, replacementpattern);
  alert("result = " + result);
</script>
 
<script type="text/javascript">
  var word="glossary term";
  var re = new RegExp("(\\b)("+word+")(?![^<]*>|[^<]*<\\/a>)","g");
  var sourcestring = 'aglossary term <a href="page.aspx">glossary term</a> glossary term <test glossary term test> glossary term';
  var replacementpattern = '$1<a href="glossary.aspx#$2" title="def" class="glossary">$2</a>';
  var result = sourcestring.replace(re, replacementpattern);
  alert("result = " + result);
</script>

Open in new window

0
 
LVL 6

Author Comment

by:mattjp88
ID: 21826983
Don't know what I did before, but your code works.  Thanks!!
0
 
LVL 82

Expert Comment

by:hielo
ID: 21831099
You are welcome!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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)
Suggested Courses

752 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