Pretty late to the party, but all the previous answers are using RegExp(s) and you don't need Regular expressions to replace an HTML tag with another one:
Simple Example - Replace h1 with h2
const oldElement = document.querySelector('h1');
// Create a new element
const newElement = document.createElement('h2');
newElement.textContent = oldElement.textContent; // Copy content from old element to new one
// Replace old element with the new one
oldElement.parentNode.replaceChild(newElement, oldElement);
Answer to the question
function replaceLiTags() {
const HTMLElementsLi = document.querySelectorAll('li');
[...HTMLElementsLi].forEach(el => {
el.parentNode.append("#" + el.textContent);
el.parentNode.append(document.createElement('br'))
el.remove();
});
}
<ul>
<li>test 1</li>
<li>test 2</li>
<li>test 3</li>
</ul>
<button onclick="replaceLiTags()">replace tags</button>
NOTE: you should always have LI as children of UL.
So what you trying to achieve is not syntactically correct since according to W3C lists are made up of sequences of list items defined by the LI element.