View Full Version : more regex drama

06-22-2012, 07:51 AM

apparently javascript doesn't allow this:

(I get an invalid group error)

but all I want is to replace everything after the first number, not including the number. I just can't seem to get my head around regex. Any hints?

Philip M
06-22-2012, 08:39 AM
var str= "abc123xyz";
str = str.replace(/(.*?\d+).*/i, "$1");
alert (str);

The new radio telescope will be the biggest in history, and will enable huge strides in gastronomy. - Reporter BBC Radio 1

06-22-2012, 01:41 PM
thanks, Philip. It's not exactly what I had in mind, though - using your example, say I wanted to replace the xyz (although xyz changes) with def. I could do:

str = str.replace(/(.*?\d+).*/i, "$1")+"def"
but isn't there some funkier way of doing it?

Philip M
06-22-2012, 02:28 PM
<script type = "text/javascript">

var str= "abc123xyz";
str = str.replace(/(.*?\d+).*/i, "$1def");
alert (str);


Not sure how to make this "funkier".

06-22-2012, 03:00 PM
that's funky enough, thanks :thumbsup: