Be sure to identify the escape characters using regular expressions in JavaScript !!!

Source: Internet
Author: User

Preface:

Some time ago, we tested the following special symbols. The javascript code is as follows:

['~! @ # $ ^ & * () = | {};: '"., [\]./? ~ @ # & * {}]

The result shows that \ cannot be matched, so the code is corrected

['~! @ # $ ^ & * () = | {};: '"., [\]./? ~ @ # & * {}]

The result shows that an error occurred while running the code in JS !!!

----------------------------------------------------------

Cause:

The symbol here does not match "\" at all,

The "[\]" shown below matches "]"


The following are two rules in the regular expression.

① […] Match any character in brackets

② "\" + Actual characters \. * +? | () {}^ $ For example: \ matched character "\"


Because the "]" that appears in the middle cannot be interpreted as an explanation, you must add an escape character before it to use it as a symbol.

Bytes -------------------------------------------------------------------------------------

Summary

1. Pay attention to escape characters in Javascript, because JavaScript will not be the same as Java. If you do not need to escape characters, an error will be reported.

Example:

If so

['~! @ # $ ^ & * () = | {};: '"., [\]./? ~ @ # & * {}]

Or

['~! @ # $ ^ & * () = | {};: '"., []./? ~ @ # & * {}]

Will invalidate the entire JS segment !!


2. Check whether the \ character is an escape !!!

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.