Re: Regular Expression Question
- From: Csaba Gabor <danswer@xxxxxxxxx>
- Date: Sat, 21 Nov 2009 12:29:08 -0800 (PST)
As does:
<script type='text/javascript'>
document.write("aaa1234567b45454bb".
replace(/.*?(\d)(?=(\d{3}))|.+$/g, "$1$2,").replace(/,+$/,""))
</script>
The second replace merely gets rid of trailing commas.
The first replace, doing the heavy lifting, says: eat chars
until there's a digit (that's the (\d), which we'll call $1)
followed by three other digits (which we'll call $2). When
that happens, replace $1 (and the prior characters) with:
$1 followed by $2 followed by ","
Characters now continue to be eaten starting from the char
following the $1 (in the original string). The eating does
not start from after the $2 because $2 was embedded within
a forward lookahead, namely the (?=...), so that the $2
chars did not get eaten in the prior part. Finally,
that |.+$ is there because once we get to the last set of 4
contiguous digits, we want to replace the remainder of the
string with the empty string (or more precisely, a comma),
so those final characters have to get eaten somehow (and
the plus ensures there is at least one character to eat).
This is a slight adaptation from the last part
of my Nov. 19 post in this same thread:
http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_frm/thread/82238d66a934a547
Csaba Gabor from Vienna
.
- References:
- Regular Expression Question
- From: Robert
- Re: Regular Expression Question
- From: Dr J R Stockton
- Re: Regular Expression Question
- From: Evertjan.
- Re: Regular Expression Question
- From: Tom Lavedas
- Re: Regular Expression Question
- From: Evertjan.
- Re: Regular Expression Question
- From: Tom Lavedas
- Re: Regular Expression Question
- From: Evertjan.
- Regular Expression Question
- Prev by Date: Re: Regular Expression Question
- Next by Date: Changing service StartMode via WMI -- why won't work?
- Previous by thread: Re: Regular Expression Question
- Next by thread: Re: Regular Expression Question
- Index(es):
Relevant Pages
|