Javascript: Regular Expression (7)

Source: Internet
Author: User
Tags uppercase letter

7. Specify the matched position

As described earlier, multiple elements in a regular expression can match one character in a string. For example, \ s matches only a blank character. Some elements in the regular expression match the positions between characters, rather than the actual characters. For example\ BMatch the boundary of a single word, that is, the boundary between the \ W (ASCII single character) character and the \ W (non-single character) character, it is either located at the boundary between an ASCII single character and the start or end of a string. Elements such as \ B do not specify the characters used in the matched string. They specify the valid location where the matching occurs. Sometimes we call these elementsRegular Expression anchorBecause they locate the pattern in a specific position in the search string. The most commonly used anchor element is ^, which positions the pattern at the beginning of the string, while the anchor element $ positions the pattern at the end of the string.

For example, to match the single word "JavaScript", you can use a regular expression/^ JavaScript $ /. If you want to retrieve the word "Java" itself (not as prefix in "JavaScript"), you can use the mode/\ sjava \ s /, it requires that there be spaces before and after the word Java. However, there are two problems:

(1) If "Java" appears at the beginning or end of a string, this mode will not match it unless there is a space at the beginning or end.

(2) When a matched string is found in this mode, the frontend and backend of the matched string returned by the string have spaces, which is not what we want.

Therefore, we use the boundary \ B of a single word to replace the real space character \ s for matching. The result expression is/\ bjava \ B /.

Element\ BThe match will be anchored in a position that is not a single word boundary. Therefore, the mode/\ B [ss] example/matches "JavaScript" and "Postscript", but does not match "script" and "scripting.

In JavaScript 1.5, you can also use any regular expression as the anchoring condition. If the symbol "(? = "And") "is added with an expression, which isForward Declaration, Specify that the following characters must be matched, but do not actually match. For example, to match the name of a commonly used programming language, but only match when there is a colon, you can use/[JJ] Ava ([ss] Ghost )? (? = \:)/. This pattern matches the word "JavaScript" in "javascript: the definitive guide",
The "Java" in a nutshell "does not match because it does not have a colon.

If you use "(?!" Introduce the Declaration and it will beAnti-Forward DeclarationTo specify that the subsequent characters do not need to be matched. For example,/Java (?! Script) ([A-Z] \ W *)/match is "Java" followed by an uppercase letter and any number of ASCII single character, but cannot follow the script ". It matches "JavaBeans", does not match "Javanese", and "javascrip", but does not match "JavaScript" or "javascripter.

The following table summarizes the regular expression anchors:

The anchor character of the regular expression.
Character Description
^ Match the start of a string. In multi-line search, match the beginning of a line
$ Match the end of a string. In multi-row search, match the end of a row.
\ B Match the boundary of a word. In short, it is located between the character \ W and \ W, or between the start or end of the character \ W and string
\ B Match the location of non-word boundary
(? = P) The Forward Declaration requires that all subsequent characters match the pattern P, but does not include those matching characters.
(?! P) The anti-Forward Declaration requires that the subsequent characters do not match the pattern P

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.