我有以下函数,它应该从输入字符串中删除标签。
示例:
'<strong>hello <em>my name <strong>is</strong> </em></strong>'
结果:
'hello my name is'
目前我有这段代码,但不确定如何使其与递归一起工作。
<小时/>const clean= (str) => {
const tags = ['<strong>', '</strong>', '<em>', '</em>', '<del>', '</del>']
let strSanited = ''
tags.forEach(tag => {
if (str.includes(tag)) {
strSanited = str.replace(tag, '')
}else{
// recursion here??
}
})
}
clean('<strong>hello <em>my name <strong>is</strong> </em></strong>')
最佳答案
您也可以使用正则表达式来执行此操作。
EX。
yourstring.replace(/<(?:.|\n)*?>/gm, '');
引用:Strip HTML from Text JavaScript
这样你就不用担心其他的html标签了,它会处理所有的html标签。如果有帮助请告诉我们
关于javascript - 使用递归删除字符串的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47677238/