본문 바로가기

I LOVE WHAT I DO

0과 1 사이, 새로운 시작!

코드 한 줄이 세상을 바꾼다.

부족하지만 최선을 다하면 된다.

욕설 필터링 만들기

by 드비디
JS
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>욕설 필터링 예제</title>
  <script>
    // 욕설 필터링 함수
    function profanityFilter(inputString) {
      // 욕설 리스트와 대체할 문자열을 객체에 저장
      const profanityList = {
        '시발': '***',
        '병신': '***',
        '새끼': '***',
        // ...
      };

      // 입력된 문자열을 공백으로 분리
      const words = inputString.split(' ');

      // 분리된 각 단어에 대해 욕설 여부를 판단
      const filteredWords = words.map((word) => {
        // 욕설 리스트에 있는 단어이면 대체 문자열로 바꾸기
        if (profanityList.hasOwnProperty(word.toLowerCase())) {
          return profanityList[word.toLowerCase()];
        }
        // 욕설이 아니면 그대로 반환
        return word;
      });

      // 필터링된 단어들을 다시 합쳐서 문자열로 반환
      return filteredWords.join(' ');
    }
  </script>
</head>

<body>
  <h1>욕설 필터링 예제</h1>
  <form>
    <label for="inputBox">입력:</label>
    <input type="text" id="inputBox" name="inputBox">
    <input type="button" value="필터링" onclick="filterInput()">
  </form>
  <p id="outputBox"></p>

  <script>
    function filterInput() {
      const inputBox = document.getElementById("inputBox");
      const outputBox = document.getElementById("outputBox");
      const filteredInput = profanityFilter(inputBox.value);
      outputBox.innerText = filteredInput;
    }
  </script>
</body>

</html>