Skip to content

Instantly share code, notes, and snippets.

@sethbergman
Created February 20, 2016 07:25
Show Gist options
  • Save sethbergman/2eb1ea0d7cce4e3c3e72 to your computer and use it in GitHub Desktop.
Save sethbergman/2eb1ea0d7cce4e3c3e72 to your computer and use it in GitHub Desktop.
Find and replace a text string with Regular Expressions in JavaScript
var regex = /Find this text string/,
replacement = 'Replace with this string';
function replaceText(i,el) {
if (el.nodeType === 3) {
if (regex.test(el.data)) {
el.data = el.data.replace(regex, replacement);
}
} else {
$(el).contents().each( replaceText );
}
}
// $('body').each( replaceText );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment