Считайте фактические заменыHtml

Программисты Html
Ответить
Anonymous
 Считайте фактические замены

Сообщение Anonymous »

Обзор < /h2>
Я создал базовый текстовый редактор с функцией «Заменить все». Тем не менее, я сталкиваюсь с проблемой, в которой вывод указывает на замену, даже когда текст остается неизменным. Вот упрощенная версия моего кода: < /p>

Код: Выделить всё

const textarea = document.getElementById('textarea');
const searchInput = document.getElementById('search');
const replaceInput = document.getElementById('replace');
const output = document.getElementById('output');
const replaceButton = document.getElementById('replaceAll');

replaceButton.addEventListener('click', () => {
const searchValue = searchInput.value;
const replaceValue = replaceInput.value;

if (!searchValue) {
output.textContent = 'No search term';
return;
}

const regex = new RegExp(searchValue, 'g');
const matches = textarea.value.match(regex);

if (matches) {
const count = matches.length;
textarea.value = textarea.value.replace(regex, replaceValue);
output.textContent = `${count} replacement${count === 1 ? '' : 's'}`;
} else {
output.textContent = 'No matches';
}
});< /code>
textarea {
width: 100%;
height: 100px;
}

#output {
color: #666;
}< /code>
apple orange apple orange


Replace all


случаи краев
нерекс-поиск
  • text>: Apple Orange Orange
  • : Apple Orange Orange
  • : li appor apport anurn /> замена : Apple
  • ожидаемый выход : "без замены" или "2 идентичных замен"
  • текущий выход : "2 замены"
regex expex
regex

regexments "

:" /> [*] text : 01.09.2021, 3/1/2022
[*] search : (\ d*)/\ d*/(\ d {4})
[*] rowled : $ 1/1/$ 2 [*] : $ 1/1/$ 2 [*] : $ 1/1/$ 2 [*] : $ 1/1/$ 2 «Нет замены» или «2 идентичных замены»
[*] текущий выход : «2 замены»

Вопрос
Как я могу изменить код, чтобы правильно обрабатывать эти края и отобразить «нет», когда текст остается ун.>

Подробнее здесь: https://stackoverflow.com/questions/794 ... placements
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Html»