Solved

use variable in regexp

Posted on 2008-06-19
7
301 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now