Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created September 7, 2020 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/e55a5cbff80cad10cb9163e6bf8a5231 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/e55a5cbff80cad10cb9163e6bf8a5231 to your computer and use it in GitHub Desktop.
Working with the new String.prototype.replaceAll method
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>String.prototype.replaceAll</title>
</head>
<body>
<main>
<h1>String.prototype.replaceAll()</h1>
<h2>Your Product Number</h2>
<p id="prod-num">AB76AF 83736E 0192DC B00371</p>
</main>
<script>
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('prod-num').addEventListener('click', convert);
if ('replaceAll' in String.prototype) {
console.log('good to go');
} else {
console.log('Not supported');
String.prototype.replaceAll = function (find, replace) {
let exp = new RegExp(find, 'g');
return this.replace(exp, replace);
};
}
});
function convert(ev) {
let prodStr = ev.target.textContent;
let prodNum = prodStr.replaceAll(' ', '-');
console.log(prodNum);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment