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

Shawn Milo shawn.milo at gmail.com
Thu May 19 14:49:02 CDT 2005


Without having tested the regex itself, I can offer the following:

Yes, you can do what you want with one regex, assuming that it matches
properly. Just do the second part first -- append to an array or fill
a string from $1, and then do the .replace part. Or even do them in
the original order, provided you do the .replace into a new string.

Shawn



On 5/19/05, Paul Novitski <paul at novitskisoftware.com> wrote:
> 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
> 
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 


-- 
Voicemail any time at:
206-666-MILO



More information about the Javascript mailing list