Пример бэкэнд -данных, которые я могу получить:
Код: Выделить всё
Foo bar bash is zoo monster poop zebra
скажем, что я пытаюсь преобразовать все слова, которые начинаются с z в href
const convertWordToHashtag = (element: BackendDataSample) => {
// extracts the text portion of the content to an array of words
const textArray = element.textContent.split(' ');
// checks if anything starts with z
textArray.forEach((word: string) => {
if (word && word.charAt(0) === 'z') {
let newElement = document.createElement('a');
newElement.href = createHrefUrl(word);
newElement.textContent = word;
// go back to the original element
element.textContent.replace(word, newElement);
}
}
}
< /code>
Но, похоже, это не вызывает ошибки замены. Это правильный подход или есть более простой способ сделать это?
Подробнее здесь: https://stackoverflow.com/questions/797 ... attributes