Here's what I'd do:
var str = "this is 'a simple' a simple 'string' string",
quoted = str.match(/'[^']+'/g);//get all quoted substrings
str =str.replace(/s/g,'p');
var restore = str.match(/'[^']+'/g);
for(var i = 0;i<restore.length;i++)
{
str = str.replace(restore[i], quoted[i]);
}
console.log(str);//logs "thip ip 'a simple' a pimple 'string' ptring"
Of course, to be clean, the code I'd actually use would be:
var str = (function(str)
{
var i,quoteExp = /'[^']+'/g,
quoted = str.match(quoteExp),
str = str.replace(/s/g, 'p'),
restore = str.match(quoteExp);
for(i=0;i<restore.length;i++)
{
str.replace(restore[i], quoted[i]);
}
return str;
}("this is 'a simple' a simple 'string' string"));