Javascript 替换字符串中所有出现的html标记和特殊unicode字符

Javascript 替换字符串中所有出现的html标记和特殊unicode字符,javascript,angularjs,Javascript,Angularjs,我有一些这样的文字 Keeping up with friends is faster than ever.<p>&#x2022; See what friends are up to<br>&#x2022; Share updates, photos and videos<br>&#x2022; Keeping up with friends is faster than ever. • See what friends are

我有一些这样的文字

Keeping up with friends is faster than ever.<p>&#x2022; See what friends are up to<br>&#x2022; Share updates, photos and videos<br>&#x2022;
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函数。以下是文档:

顾名思义,jQuery文本函数将HTML作为文本读取

var asText=$(“#示例”).text();
$(“asText”).text(asText);
$(“#asTextLength”).text(asText.length)

&x2022;举例及;Test

唯一“正确”(完整)的方法是将文档解析为HTML DOM,然后创建一些内容来读取DOM并将其格式化为“简单”的定义

除了Javascript之外,还有很多工具可以实现这一点,之前的StackOverflow交流讨论了其中一些工具:

我认为在Javascript中“滚动你自己的”将是痛苦的,除非你设置了一个非常低的标准,并且只努力“美化”非常有限的HTML

也就是说,为什么你不能继续使用你现有的一种工具,不断地加入“规则”来处理你想处理的事情呢

str.replace(/&#x2022;/gm, '•')

等等,等等,等等,雅达雅达雅达,ad nauseum…

您可以使用ng绑定html,例如

<p ng-bind-html="myHTML"></p>

$scope.myHTML = 'Keeping up with friends is faster than ever.<p>&#x2022; See what friends are up to<br>&#x2022; Share updates, photos and videos<br>&#x2022;';

$scope.myHTML='与朋友保持联系比以往任何时候都快。&x2022;看看朋友们在干什么;共享更新、照片和视频;
或。。。使用Sarhanis建议的jQuery!(我不知道jQuery可以做到这一点……妙极了!)有没有办法将所有正则表达式的替换组合成一个。因为我需要做:
str=str.replace(/&x2022;/gm,“•”)
str=str.replace(/\s/g,”)
possibility@ShankarRegmi:我很确定您可以将所有这些
.replace()
调用串在一起:
str.replace(A,B).replace(C,D).replace(E.F)
。试试看。