<header>
  <h2 class="title">String.prototype.replace()</h2>
  <p class="description">Метод замінює частини рядка на вказане значення, що збігається з регулярним виразом або рядком.</p>
</header>
<main>
  <div class="result">
    <!-- Поля для введення тексту та налаштувань заміни -->
    <label for="inputText">Введіть текст:</label>
    <textarea id="inputText" rows="4" placeholder="Введіть ваш текст тут..."></textarea>

    <label for="searchText">Знайти слово або фразу:</label>
    <input type="text" id="searchText" placeholder="Слово або фраза для заміни">

    <label for="replaceText">Замінити на:</label>
    <input type="text" id="replaceText" placeholder="Новий текст">

    <!-- Кнопка для виконання заміни -->
    <button id="replaceButton">Замінити</button>

    <!-- Відображення результату -->
    <h3>Результат:</h3>
    <p id="outputText">Результат з'явиться тут...</p>
  </div>
</main>
body {
  font-size: 16px;
  line-height: 1.5;
  font-family: monospace;
}

header {
  background-color: #f1f1f1;
  margin-bottom: 25px;
  padding: 15px;
  -webkit-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  -moz-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}

header h2.title {
  padding-bottom: 15px;
  border-bottom: 1px solid #999;
}

header p.description {
  font-style: italic;
  color: #222;
}

.result {
  background-color: #f8f8f8;
  padding: 15px;
  -webkit-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  -moz-box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
  box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}

textarea, input[type="text"] {
  width: 100%;
  padding: 8px;
  margin-top: 10px;
  margin-bottom: 15px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

#replaceButton {
  padding: 10px 15px;
  border: none;
  background-color: #3498db;
  color: white;
  cursor: pointer;
  border-radius: 4px;
}

#outputText {
  margin-top: 15px;
  font-weight: bold;
  color: #333;
  white-space: pre-wrap;
}
const inputText = document.getElementById('inputText');
const searchText = document.getElementById('searchText');
const replaceText = document.getElementById('replaceText');
const replaceButton = document.getElementById('replaceButton');
const outputText = document.getElementById('outputText');

// Функція для виконання заміни тексту
replaceButton.addEventListener('click', function() {
  const text = inputText.value;
  const searchValue = searchText.value;
  const replaceValue = replaceText.value;

  // Використання методу replace для заміни
  const newText = text.replace(new RegExp(searchValue, 'g'), replaceValue);

  // Виведення результату на екран
  outputText.textContent = newText;
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.