Re: Regular Expression - Match all except last one
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Regular Expression - Match all except last one

From: Alexander Bartolich <alexander.bartolich@gmx.at>
Date: Sun Mar 19 2006 - 22:33:48 CET

Randy Webb wrote:
> I know that the /g flag will match all occurrences. Is there a way, with
> a Regular Expression, to match all occurrences *except* the last one?
>
> pattern = /df/g;
> var myString = "asdfasdfasdfasdf";
> var newString = myString.replace(pattern,'gh');
> alert(newString)
>
> Gives me: asghasghasghasgh as it should.
> What I want: asghasghasghasdf Where the last one is not replaced.

var newString = myString.replace(/df(.)/g,'gh$1');

-- 
Received on Mon May 1 04:15:11 2006