JS Convert HTML escape character

Source: Internet
Author: User

Today in the Wangeditor editor to write the background Management article page, after saving the article need to call an edit draft, this time need to put the content of the editor to the editor, the results output my label:<p> Hello </p> after my Console.log ( data) = = "&lt;p&gt; Punching sandbags &lt;/p&gt; "。 Yes, my <> is escaped, because there is no way to upload a word without escaping it.

And then there's the regular I'm looking for from somewhere else. Ha haha ~

Remove the HTML tag function removehtmltab (tab) {return tab.replace (/<[^<>]+?>/g, ");//delete all HTML tags}

  

Normal character converted to Html2escape function (sHtml) {return shtml.replace (/[<>& "]/g,function (c) {return {' < ': ' < ', ' > ': ' > ', ' & ': ' & ', ' ' ' ': ' ' '}[c ';}

I am using the following, just fine. hehe ~

Convert the signifier to normal character function escape2html (str) {var arrentities={' lt ': ' < ', ' GT ': ' > ', ' nbsp ': ', ' amp ': ' & ', ' quot ': ' "' }; Return Str.replace (/& (Lt|gt|nbsp|amp|quot);/ig,function (all,t) {return arrentities[t];});

  

Turn into a space function nbsp2space (str) {var arrentities = {' nbsp ': '}; return Str.replace (/& (nbsp);/ig, function (all, t) {return arrentities[t]})}

  

Carriage return to BR label function RETURN2BR (str) {return str.replace (/\r?\n/g, "<br/>");}

  

Removes the opening end wrap and converts 3 or more consecutive lines into 2 newline function trimbr (str) {Str=str.replace (/((\s|) *\r?\n) {3,}/g, "\r\n\r\n");//Limit up to 2 newline str=str.replace (/^ (\s|) *\r?\n) +/g, ');//Clear Start newline str=str.replace (/((\s|) *\r?\n) +$/g, ');//clear end newline return str;}

  

Combine multiple contiguous spaces into a single space function Mergespace (str) {str=str.replace (/(\s|) +/g, "); return str;}

  

JS Convert HTML escape character

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.