[Javascript] regexp - replace & capture with one operation?

Paul Novitski paul at novitskisoftware.com
Thu May 19 14:38:46 CDT 2005


Here's my regexp question:

1) I want to transform this source text:

"This text contains {ix_key|index terms} to be processed."

...into this:

"This text contains <a class="ix_key" href="#ix_key">index terms</a> to be 
processed."

The key becomes the class and the local anchor link, and the term becomes 
embedded in the anchor tag.

My first stab at a regexp would be:

.replace(/\{(.)*\|(.)*\}/, "<a class="$1" href="#$1">$2</a>")


2) I also want to collect each key (e.g., "ix_key") for further 
processing.  If performed separately, I figure the regexp for this would be:

.match(/\{(.)*\|.*\}/)

to generate an array of keys or key locations.

Do I have to do this with two regexp operations, one to locate & "capture" 
the keys and another to mark up the keys and terms with HTML?

Thanks,
Paul





More information about the Javascript mailing list