Created
September 7, 2020 20:58
-
-
Save prof3ssorSt3v3/e55a5cbff80cad10cb9163e6bf8a5231 to your computer and use it in GitHub Desktop.
Working with the new String.prototype.replaceAll method
This file contains 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
<!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