简体   繁体   English

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

[英]Replace all occurrences of html tags and special unicode character from a string

I have some text like this 我有一些像这样的文字

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;

And I want text like this 我想要这样的文字

Keeping up with friends is faster than ever.
 • See what friends are up to
 • Share updates, photos and videos

I cleaned out html tags by str.replace(/<[^>]+>/gm, '') , however I cannot remove special symbols. 我通过str.replace(/<[^>]+>/gm, '')清除了html标签,但是我无法删除特殊符号。

I am calculating the length of string so special character before compiling in browser gave me different length. 我正在计算字符串的长度,所以在浏览器编译之前特殊字符给了我不同的长度。

Thanks 谢谢

In this case, it's best to use an external library, like jQuery, to help you out. 在这种情况下,最好使用外部库(如jQuery)来帮助您。

In my example below, I'm using the jQuery function called text(). 在下面的示例中,我使用名为text()的jQuery函数。 Here's the documentation: http://api.jquery.com/text/ 这是文档: http//api.jquery.com/text/

As the name suggests, the jQuery text function reads the HTML as 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">&#x2022; Ex<em>ample</em> &amp; <span>T<strong>est</strong></span></p> <p id="asText"></p> <p id="asTextLength"></p> 

The only "right" (complete) way to do this is to parse the document in to an HTML DOM, then create something that will read the DOM and format it to your definition of "simple". 执行此操作的唯一“正确”(完整)方法是将文档解析为HTML DOM,然后创建将读取DOM并将其格式化为“简单”定义的内容。

There are many tools out there to do this outside of Javascript, and prior StackOverflow exchanges discuss some of them: 有许多工具可以在Javascript之外执行此操作,之前的StackOverflow交换讨论了其中一些:

I think "rolling your own" in Javascript will be painful, unless you set a VERY low bar, and only endeavor to "prettyify" a very limited amount of HTML. 我认为在Javascript中“滚动你自己”将是痛苦的,除非你设置了非常低的标准,并且只努力“非常化”非常有限的HTML。

That being said, why can't you just continue with the one tool you've got, and keep putting in "rules" to handle what you want to handle? 话虽这么说,为什么你不能继续使用你所拥有的一个工具,并继续加入“规则”来处理你想要处理的东西?

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

etc., etc., etc., yada yada yada, ad nauseum ... 等等,yada yada yada, ad nauseum ...

You can use ng-bind-html eg 你可以使用ng-bind-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;';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM