See the Pen 문자 대체 by nilgi (@nilgi) on CodePen.
<h3>replace()는 문자를 바꿈</h3>
<p id="np1">인생은 찰나다.</p>
<button onclick="nilgif()">삶의 선택</button>
<script>
function nilgif() {
let text = document.getElementById("np1").innerHTML;
document.getElementById("np1").innerHTML =
text.replace("찰나", "반복이");
// 찰나를 반복이로 바꾼다.
}
</script>
text.replace("닐기", "우주");
// 첫번째 문자부터 바꾼다.
text.replace("ab", "A+B");
// 대소문자를 구별한다.
text.replace(/ab/i, "A+B");
// 대소문자를 구별하지 않으려면 /문자/i
text.replace(/AB/g, "ab");
// 모든 글자를 바꾸려면 /문자/g
text.toUpperCase();
// 소문자를 대문자로
text.toLowerCase();
// 대문자를 소문자로
어렵다... 그만할까? ㅠ-ㅠ