Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] regexp - replace & capture with one operation?
- From: shawn.milo at gmail.com (Shawn Milo)
- Subject: [Javascript] regexp - replace & capture with one operation?
- Date: Thu May 19 14:49:11 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@xxxxxxxxxxxxxxxxxxxx> 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@xxxxxxxxxx
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
--
Voicemail any time at:
206-666-MILO
- References:
- [Javascript] regexp - replace & capture with one operation?
- From: Paul Novitski
- [Javascript] regexp - replace & capture with one operation?
- Prev by Date: [Javascript] regexp - replace & capture with one operation?
- Next by Date: [Javascript] RegEx for validating a file name
- Previous by thread: [Javascript] regexp - replace & capture with one operation?
- Next by thread: [Javascript] regexp - how to exclude a substring?
- Index(es):