Created
December 17, 2020 02:05
-
-
Save tuantvk/36a315da30830c445d01d6e1fbb65aa7 to your computer and use it in GitHub Desktop.
Strip HTML tags from string in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cleanTagHtml(str) { | |
if (str && str.trim().length) { | |
let data = []; | |
let arr = str | |
.replace(/<\/?[^>]+(>|$)/g, "") | |
.split(" "); | |
// break one line when multiple | |
for (let i = 0; i < arr.length; i++) { | |
if (arr[i] && arr[i].trim()) { | |
data.push(arr[i]); | |
} else if (arr[i - 1] && arr[i - 1].trim()) { | |
data.push("\n"); | |
} | |
} | |
return data.join(""); | |
} | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, It do not strip <a href tags