stripping

Remove html tags except <br> or <br/> tags with javascript

血红的双手。 提交于 2019-11-28 20:32:57
I want to remove all the html tags except <br> or <br/> tags from a string using javascript. I have seen many questions like this but their answers will remove all the html tags including <br> and <br/> tags. Does anyone knows a regex to do this? Try This function remove_tags(html) { var html = html.replace("<br>","||br||"); var tmp = document.createElement("DIV"); tmp.innerHTML = html; html = tmp.textContent||tmp.innerText; return html.replace("||br||","<br>"); } Use a negative lookahead (by using a regex such as /<(?!br\s*\/?)[^>]+>/g ): var html = 'this is my <b>string</b> and it\'s pretty

Remove html tags except <br> or <br/> tags with javascript

橙三吉。 提交于 2019-11-27 20:44:02
问题 I want to remove all the html tags except <br> or <br/> tags from a string using javascript. I have seen many questions like this but their answers will remove all the html tags including <br> and <br/> tags. Does anyone knows a regex to do this? 回答1: Try This function remove_tags(html) { var html = html.replace("<br>","||br||"); var tmp = document.createElement("DIV"); tmp.innerHTML = html; html = tmp.textContent||tmp.innerText; return html.replace("||br||","<br>"); } 回答2: Use a negative