[thelist] regex help

Kristof Neirynck k.neirynck at belgacom.net
Wed Sep 3 07:47:50 CDT 2003


> I have what is probably a simple regex to do but i cannot figure it
> out. Mainly because I need a variable in my regex. Here is my target
> string:
> 
> string = "s1s2s3s4";
> 
> from there, I need to remove say "s3".
> 
> But the 3 is going to be a variable, say personID. so really I need
> to remove "s" + personID as my regex.

this should do fine: ^(.*)(s3)(s.*|)$

simple Javascript example:

var theString = 's1s2s3s4s45s201s10s9';
var theId = 201;
var theRegexString = '^(.*)(s' + theId + ')(s.*|)$';
var theRegex=new RegExp(theRegexString );
var theNewString = theString.replace(theRegex, '$1$3');
/*
$1 -> s1s2s3s4s45
$2 -> s201
$3 -> s10s9
*/
/*
alternative:
var theNewString = theString;
if(theString.match(theRegex)){
   theNewString = RegExp.$1 + RegExp.$3;
}
*/




HTH
Kristof





More information about the thelist mailing list