Backslashes are escaped in Java and JS (javaScript)

Source: Internet
Author: User

first, Why do you want to escape? Some characters in the regular have special meanings, for example * can be any number of times of the previous Subexpression. Represents any single character except "\ r \ n", which represents one or more occurrences of the previous subexpression, and so On. In some cases, it is necessary to validate these characters in a regular way, for example to verify the subtraction four operation symbols, in order to tell the compiler that you want to verify a character rather than a regular expression, you have to escape the special meaning of the character, so that you have escaped this Operation.

In both Java and javascipt, the backslash "\" is escaped, and then the usage of the two languages varies, as illustrated Below:

1. Use of literal characters in Java

In java, a backslash is represented by two backslashes, for example, "\ \" means a backslash "\", the first backslash is escaped, and the second is the backslash itself. Let's look at an example:

Pattern pattern=pattern.compile ("\ \.") ); System.out.println (pattern); // output/.

This is to verify the Point.

So to verify a backslash, you need four backslashes \\\\

Pattern pattern=pattern.compile ("\\\\"); System.out.println (pattern); // output \ \

To verify a backslash character:

Pattern pattern=pattern.compile ("[\\\\]"); System.out.println (pattern); // the output [\ \] means that you want to validate the backslash itself String a= "\ \"; System.out.println ("a=" +a); // output a=\, which represents a backslash character Matcher matcher=pattern.matcher (a); System.err.println (matcher.matches ()); // output True to verify that the

Verify any number of times 1

Pattern Pattern=pattern.compile ("^1*$"); // verify any number of times 1 System.out.println (pattern); // Output ^[-+*/]$ String a= "1111"; System.out.println ("a=" +a); // Output a=1111 Matcher matcher=Pattern.matcher (a); System.err.println (matcher.matches ()); // output true, validation passed through validation            

Verifying the "1*" string

Pattern Pattern=pattern.compile ("^1\\*$"); // verify any number of times 1 System.out.println (pattern); // Output ^1\*$ String a= "1*"; System.out.println ("a=" +a); // Output a=1* Matcher matcher=Pattern.matcher (a); System.err.println (matcher.matches ()); // output True to verify that the

Verify +-*/

The minus sign puts the first bit, does not need to escape

Pattern Pattern=pattern.compile ("^[-+*/]$"); // System.out.println (pattern); // Output ^[-+*/]$ String a= "/"; System.out.println ("a=" +a); // output a=-, representing a minus character Matcher matcher=Pattern.matcher (a); System.err.println (matcher.matches ()); // output True to verify that the

The minus sign is not the first bit and needs to be escaped because: [0-9] in the regular expression means 0 to 9 any character

Pattern Pattern=pattern.compile ("^[+\\-*/]$"); // System.out.println (pattern); // Output ^[+\-*/]$ String a= "-"; System.out.println ("a=" +a); // output a=-, representing a minus character Matcher matcher=Pattern.matcher (a); System.err.println (matcher.matches ()); // output True to verify that the

2. Regular escape in JS

Escaping directly with a backslash

var str=/^\\$/;  // Verify a backslash var teststr= ' \ \ '; console.log (str.test (teststr))// output True

Verify +-*/

var str=/^[+\-*/]$/;  Verify +-*/var teststr= '-'; console.log (str.test (teststr))// output True

Backslashes are escaped in Java and JS (javaScript)

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.