Skip to content

Instantly share code, notes, and snippets.

@orasik
Last active March 14, 2017 17:58
Show Gist options
  • Save orasik/a0e1f4658d05c96150c960980b80dd6f to your computer and use it in GitHub Desktop.
Save orasik/a0e1f4658d05c96150c960980b80dd6f to your computer and use it in GitHub Desktop.
Javascript, search for a string using regex in a specific tag and replace it with mathematical value
// This function basically will search for prices per hour in AWS webpage and replace it with Monthly price
function findAndReplace(tag) {
var content = document.getElementsByTagName(tag)[0].innerHTML;
// This pattern will search for strings like `$0.023 per Hour`
var pattern = /\$\d+\.\d+\sper\sHour/g;
content = content.replace(pattern,
function(entry) {
// This will extract float value only
pattern = /\d+\.\d+/g
return '$'+ 24 * 30 * parseFloat(entry.match(pattern)) + ' per Month'
}
);
document.getElementsByTagName(tag)[0].innerHTML = content;
}
findAndReplace("main"); // will search for all strings matches in <main> tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment