Regular matching in parentheses

Source: Internet
Author: User

In a regular expression, the brackets in front [belong to a special character and need to be escaped to match it, that is, \ is added; then the brackets] do not belong to a special character and do not need to be escaped. Var pattern = "(\ [{1})"; // error, correct: ([\ [] {1}) var flag = "gi "; var reg = new RegExp (pattern, flag); reg.exe c ("<> []"); alert (RegExp. $1); The above code is incorrect. The error is in pattern and should be written ([\ [] {1 }). That is, for the brackets [, in addition to escape characters, they should also be enclosed in brackets. It seems that it is not uncommon to deal with the brackets. Var pattern = "([\] {1})"; // error, correct: (] {1}) var flag = "gi "; var reg = new RegExp (pattern, flag); reg.exe c ("<> []"); alert (RegExp. $1); The above code is correct in IE, but in Chrome, it does not get the expected result, the error is also in pattern, should be written (] {1 }). That is, for brackets], it should be written directly, neither escape characters nor brackets. Material: special characters (escape required) in the regular expression ^ match the starting position of the input string, unless used in the square brackets expression, it indicates that the character set combination is not accepted. To match the ^ character itself, use \ ^. $ Matches the end position of the input string. If the Multiline attribute of the RegExp object is set, $ also matches '\ n' or' \ R '. To match the $ character, use \ $.. Match any single character except linefeed \ n. To match., use \. \ Mark the next character as a special character, or a literal character, or backward reference, or an octal escape character. For example, 'n' matches the character 'n '. '\ N' matches the line break. The sequence '\' matches "\", while '\ (' matches "(". | Specify an option between the two items. To match |, use \ |. {Mark the start of the qualifier expression. To match {, use \{. [Mark the start of a bracket expression. To match [, use \[. (And) mark the start and end positions of a subexpression. Subexpressions can be obtained for future use. To match these characters, use \ (and \). * Matches the previous subexpression zero or multiple times. To match * characters, use \*. + Match the previous subexpression once or multiple times. To match + characters, use \ +.? Match the previous subexpression zero or once, or specify a non-Greedy qualifier. To match? Character, use \?.

 

Related Article

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.