Atitit.js string content escape JS processing html

Source: Internet
Author: User

Atitit.js string content escape js processing HTML

1. js processing HTML problem 1

2. js string content escape 1

2.1. Process Flow 1

3. The following table lists the remaining special characters, which can be added to a text string using a backslash: 2

4.--code 2

1. jsprocessingHTMLthe problem

Anti-slash problem

Quotation mark problem. The statement interruption issue caused by carriage return:

2. jsthe string content escaped

A carriage return

There's a newline escape.

One quote escape:

Anti-Slash escape

2.1. Processing process

Replace the backslash first. In quotation marks, in carriage return, in line break:

Author:: Old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

3. The following table lists the remaining special characters that can be added to a text string using a backslash:

Code

Output

\‘

Single quotation marks

\"

Double quotes

\&

and number

\\

Back slash

\ n

Line break

\ r

Carriage return character

\ t

Tabs

\b

Backspace

\f

Page break

4.--code

Public class encodex {

Public static void main (string[] args) {

String s = "Window.document.getElementById" (' Editor '). value= ' \ "aaa\" ' " ;

s =filex. Read ("C:\\1.txt");

System. out . println (jsencode( s));

}

Public static string Jsencode (String p2_txt) {

//  TODO auto-generated Method Stub

String Doublequoto = "\"" ;

String ReplaceAll  =  P2_txt . ReplaceAll (doublequoto, "\\\\\");

return replaceall;

}

Public static string jsencodesinglequoue ( string p2_ TXT) {

//  TODO auto-generated Method Stub

P2_txt  =  P2_txt . Replace ("\ \", "\\\\");

String doublequoto="\";

String replaceall = p2_txt. ReplaceAll (doublequoto, "\ \\\\ ' ");   

replaceall = replaceall. ReplaceAll ("\ r"),  "\\\\r");

replaceall = replaceall. ReplaceAll ("\ n", "\\\\ N ");

return replaceall;

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Atitit.js string content escape JS processing html

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.