Created
February 20, 2016 07:25
-
-
Save sethbergman/2eb1ea0d7cce4e3c3e72 to your computer and use it in GitHub Desktop.
Find and replace a text string with Regular Expressions in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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