PDA

View Full Version : replace regexp partial replace


dumdummy
02-22-2010, 01:40 PM
Hi

I am using a regular expression to replace a string pattern

I want to find the pattern but only replace a part of it, leaving the rest intact

is that possible using replace and regexp :confused:

koko5
02-22-2010, 08:32 PM
Maybe if you specify input and desired output (plus your current regex if any) maybe someone helps :)

A1ien51
02-23-2010, 04:56 AM
Use groups

var str = "123456789 12456789";
var re = /(3)(456)(7)/g;
var newStr = str.replace(re,"$1aaa$3");


$1 = 3
$2 = 456
$3 = 7


Eric

Philip M
02-23-2010, 09:00 AM
If your pattern is a string, then:-

var str = "The quick brown fox jumps over the lazy dog";
var re = /(quick)( brown )(fox)/g;
var newStr = str.replace(re,"$1 orange $3");
alert (newStr);

var newStr1 = str.replace(/brown/g, "orange")
alert (newStr1);

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

dumdummy
03-31-2010, 06:52 PM
cheers :)