Update Find and Replace text

This commit is contained in:
LSvekis
2021-09-11 14:44:29 -04:00
committed by GitHub
parent 143b56d64f
commit 59b4952d6c
+14 -11
View File
@@ -1,26 +1,28 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
    <title>Complete JavaScript Course</title> <title>Complete JavaScript Course</title>
</head> </head>
<body> <body>
    <div id="output">Complete JavaScript Course</div> <div id="output">Complete JavaScript Course</div>
Search for : Search for :
    <input id="sText" type="text"> <input id="sText" type="text">
    <br> Replace with : <br> Replace with :
    <input id="rText" type="text"> <input id="rText" type="text">
    <br> <br>
    <button>Replace</button>> <button>Replace</button>
<script> <script>
const output = document.getElementById('output') const output = document.getElementById("output")
const findValue = document.getElementById('sText'); const findValue = document.getElementById("sText");
const replaceValue = document.getElementById('rText'); const replaceValue = document.getElementById("rText");
document.querySelector("button").addEventListener("click", lookUp); document.querySelector("button").addEventListener("click", lookUp);
function lookUp() { function lookUp() {
const s = output.textContent; const s = output.textContent;
const rt = replaceValue.value; const rt = replaceValue.value;
const re = new RegExp(findValue.value, "gi"); const re = new RegExp(findValue.value, "gi");
if (s.match(re)) { if (s.match(re)) {
let newValue = s.replace(re, rt); let newValue = s.replace(re, rt);
output.textContent = newValue; output.textContent = newValue;
@@ -28,4 +30,5 @@
} }
</script> </script>
</body> </body>
</html> </html>