[英]Replace all occurrences of html tags and special unicode character from a string
我有一些像这样的文字
Keeping up with friends is faster than ever.<p>• See what friends are up to<br>• Share updates, photos and videos<br>•
我想要这样的文字
Keeping up with friends is faster than ever.
• See what friends are up to
• Share updates, photos and videos
我通过str.replace(/<[^>]+>/gm, '')
清除了html标签,但是我无法删除特殊符号。
我正在计算字符串的长度,所以在浏览器编译之前特殊字符给了我不同的长度。
谢谢
在这种情况下,最好使用外部库(如jQuery)来帮助您。
在下面的示例中,我使用名为text()的jQuery函数。 这是文档: http : //api.jquery.com/text/
顾名思义,jQuery文本函数将HTML作为文本读取。
var asText = $("#example").text(); $("#asText").text(asText); $("#asTextLength").text(asText.length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <p id="example">• Ex<em>ample</em> & <span>T<strong>est</strong></span></p> <p id="asText"></p> <p id="asTextLength"></p>
执行此操作的唯一“正确”(完整)方法是将文档解析为HTML DOM,然后创建将读取DOM并将其格式化为“简单”定义的内容。
有许多工具可以在Javascript之外执行此操作,之前的StackOverflow交换讨论了其中一些:
我认为在Javascript中“滚动你自己”将是痛苦的,除非你设置了非常低的标准,并且只努力“非常化”非常有限的HTML。
话虽这么说,为什么你不能继续使用你所拥有的一个工具,并继续加入“规则”来处理你想要处理的东西?
str.replace(/•/gm, '•')
等等,yada yada yada, ad nauseum ...
你可以使用ng-bind-html例如
<p ng-bind-html="myHTML"></p>
$scope.myHTML = 'Keeping up with friends is faster than ever.<p>• See what friends are up to<br>• Share updates, photos and videos<br>•';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.