JS Coding & decoding &html escape __arcinfo

Source: Internet
Author: User

JS Transform HTML escape character

Get rid of HTML tags

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

Convert ordinary character to signifier

function Html2escape (sHtml) {return
 shtml.replace (/[<>&)]/g,function (c) {return {' < ': ' &lt; ', ' > ': ' &gt; ', ' & ': ' &amp; ', ' ' ': ' &quot; '} [c];});

Convert symbol to ordinary 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 to 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/>");
}

Remove the beginning end line break and convert 3 consecutive lines to 2 lines of wrapping

function Trimbr (str) {
 str=str.replace ((\s|&nbsp;) *\r?\n) {3,}/g, "\r\n\r\n");/limit up to 2 lines of wrapping
 str= Str.replace ((\s|&nbsp;) *\r?\n) +/g, ')//purge opening line/^
 ((str=str.replace) \s|&nbsp;) *\r?\n, "); Clear end Line wrap return
 str;
}

Combine multiple contiguous spaces into one space

function Mergespace (str) {
 str=str.replace (/(\s|&nbsp;) +/g, ");
 return str;
}

JS Code decoding
I. Definition and usage
The encodeURI () function encodes a string as a URI.

Grammar
encodeURI (uristring)
Parameter description
uristring required. A string that contains the URI or other text to encode.
return value
A copy of the uristring in which some of the characters will be replaced by the hexadecimal escape sequence.

Description
The method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks:-_. ! ~ * ' ().

The purpose of this method is to encode the URI completely, so that the encodeURI () function is not escaped for the following ASCII punctuation that has special meaning in the URI:;/?:@&=+$,#

Tips and comments
Tip: If the URI component contains delimiters, for example? and #, you should encode each component separately using the encodeURIComponent () method.

The decoding of this method is decodeURI ()

II. Definitions and usage

The escape () function encodes the string so that it can be read on all computers.

Grammar
Escape (String)
Parameter description
String required. The string to be escaped or encoded.
return value
A copy of the encoded string. Some of these characters are replaced with the 16-in escape sequence.

Description
The method does not encode ASCII letters and numbers, and does not encode the following ASCII punctuation:-_. ! ~ * ' (). All other characters will be replaced by escape sequences.

Tips and comments
Tip: You can use Unescape () to decode an escape () encoded string.

Note: ECMAScript v3 against using this method, the application uses decodeURI () and decodeuricomponent () to replace it.

Three, JavaScript encodeuricomponent () function

Definitions and usage
The encodeURIComponent () function encodes a string as a URI component.

Grammar
encodeURIComponent (uristring)
Parameter description
uristring required. A string that contains the URI component or other text to encode.
return value
A copy of the uristring in which some of the characters will be replaced by the hexadecimal escape sequence.

Description
The method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks:-_. ! ~ * ' ().

Other characters (such as:;/?:@&=+$,# the punctuation marks used to separate the URI components) are replaced by one or more hexadecimal escape sequences.

Tips and comments
Tip: Note the difference between the encodeURIComponent () function and the encodeURI () function, which assumes that its arguments are part of the URI (such as protocol, host name, path, or query string). Therefore, the encodeURIComponent () function escapes the punctuation marks used to separate parts of the URI.
This method decodes the way decodeuricomponent

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.