使用正则表达式删除特殊字符

时间:2019-10-28 21:04:57

标签: javascript regex

伙计们。我有一个包含很多特殊字符的特定字符串,需要删除。

这是字符串:

let str = 'The start tag must have a matching end tag.  An explicit end tag can be provided by adding </cfscript>.If the body of the tag is empty, you can use the shortcut <cfscript .../>.<p>The CFML compiler was processing:<ul><li>A cfscript tag beginning on line 90, column 10.<li>A cfscript tag beginning on line 90, column 10.</ul>'

这是我的正则表达式,它不起作用: var regex = str.replace(/<|>|\/|\/ul|\/li|ul&|li&/g,'')

任何帮助都会很棒!谢谢

1 个答案:

答案 0 :(得分:3)

您正在尝试剥离转义的HTML标记(<是&lt;>是&gt;),以及它们之间的所有内容(请参见regex101):

const str = 'The start tag must have a matching end tag.  An explicit end tag can be provided by adding &lt;/cfscript&gt;.If the body of the tag is empty, you can use the shortcut &lt;cfscript .../&gt;.&lt;p&gt;The CFML compiler was processing:&lt;ul&gt;&lt;li&gt;A cfscript tag beginning on line 90, column 10.&lt;li&gt;A cfscript tag beginning on line 90, column 10.&lt;/ul&gt;'

const result = str.replace(/&lt;.+?&gt;/g,'')

console.log(result)