Skip to content

Instantly share code, notes, and snippets.

@scottmas
Created May 6, 2015 05:46
Show Gist options
  • Save scottmas/daed992544354a4bf737 to your computer and use it in GitHub Desktop.
Save scottmas/daed992544354a4bf737 to your computer and use it in GitHub Desktop.
String replace but ignore quoted section
//Exactly like String.replace except it ignores matches inside of single or double quotes
function replaceIgnoreQuoted(string, find, replaceOrCb) {
var quotedString = /(["'])(?:\\?.)*?\1/g;
var indexOfStrings = [],
match;
quotedString.lastIndex = 0;
while(match = quotedString.exec(string)) {
indexOfStrings = indexOfStrings.concat([match.index, quotedString.lastIndex]);
}
var ret = string.replace(find, function(matched) {
if(!isQuoted(arguments[arguments.length - 2], indexOfStrings)) {
if(typeof replaceOrCb === 'string') {
return replaceOrCb
} else {
return replaceOrCb(matched, index, original)
}
} else {
return matched;
}
});
return ret;
function isQuoted(startIndex, indexOfStrings) {
var isQuoted = false;
for(var i = 0; i < indexOfStrings.length; i = i + 2) {
if(startIndex > indexOfStrings[i] && startIndex < indexOfStrings[i + 1]) {
isQuoted = true;
break;
}
}
return isQuoted;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment