This regex
var reg = <font[^>]*>(?:\s*<(\w+)>\s*<\/
This regex
var reg = <font[^>]*>(?:\s*<(\w+)>\s*<\/\1>\s*|\s*)<\/font>
is being used in replace(reg, '') to remove empty font tags. It removes this
正在替换(reg,'')以删除空字体标记。它删除了这个
<font size="3"></font>
okay.
How can I modify it so it will remove
如何修改它以便删除
<font size="3"> </font>
or
<font size="3"> </font>
or
<font size="3"> </font>
I want to be able to find all empty font tags with any number of spaces between the tags and replace with an empty string.
我希望能够找到标签之间有任意数量空格的所有空字体标签,并用空字符串替换。
2 个解决方案
#1
0
If all you want to do is find all empty font tags with any number of spaces between the tags and replace with an empty string, you can simplify your regex a bit. If as you've indicated with your current regex, you also want to catch one layer of empty tags within it, you can do as follows:
如果你想要做的就是找到所有空字体标签,标签之间有任意数量的空格,并用空字符串替换,你可以稍微简化你的正则表达式。如果您已经使用当前的正则表达式进行了指示,那么您还希望在其中捕获一层空标记,您可以执行以下操作:
var reg = /<font[^>]*>(?:\s| |<(\w+)>(?:\s| )*<\/\1>)*<\/font>/gi
var testStr = `<font size="3"></font>
<font size="3"> </font>
<font size="3"> </font>
<font size="3"> </font>
<font size="3"> </font>
<font size="3"><something> </something></font>`
console.log(testStr.replace(reg, ""))
#2
0
try this
<font[^>]*>(?:\s*<(\w+)>\s*<\/\1>\s*|\s*|( )+)<\/font>
as per your regex. it already catches spaces. I just added the nbsp;
根据你的正则表达式。它已经占据了空间。我刚加入了
regex101 link: https://regex101.com/r/4Msyv1/2
regex101链接:https://regex101.com/r/4Msyv1/2
>\s*|\s*)<\/font>
var reg = <font[^>]*>(?:\s*<(\w+)>