JavaScript-A simple problem with a non-capturing grouping

Source: Internet
Author: User
When I read the jquery code, I see a regular expression definition
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/
Rsingletag is used to match simple HTML tags,
There is a non-capturing group: (?:<\/\1>), here's the question:
Why use non-capturing groupings, and what if not?

For example, you can also use a non-capturing grouping in this regular expression:
rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/

Why use non-capturing groupings?
Where is the regular expression used?

Reply content:

When I read the jquery code, I see a regular expression definition
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/
Rsingletag is used to match simple HTML tags,
There is a non-capturing group: (?:<\/\1>) The problem is:
Why use non-capturing groupings, and what if not?

For example, you can also use a non-capturing grouping in this regular expression:
rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/

Why use non-capturing groupings?
Where is the regular expression used?

Then ask yourself what is a non-capturing group, and where do you know this "noun"?
So the question is, what's a non-capturing group?

Is the grouping you don't want to be caught in, in JS, the character that a brace is wrapped in is a grouping. Sometimes you have to use parentheses to group the same type of characters when writing a regular, for example /(abc){3}/ , ABC three letters match three times, and ABC is the same type of character. However, this set of characters is not required in match or exec or replace and so on, so it is not captured. Think about the following demand
There is a string, using n ABCD plus a section of other characters, and now I want to write a string of n abcd behind

For example abcdabcdabcdefg , you want EFG.

'abcdabcdabcdefg'.match(/(abcd)+(.+)/) => ["abcdabcdabcdefg", "abcd", "efg"]'abcdabcdabcdefg'.match(/(?:abcd)+(.+)/) => ["abcdabcdabcdefg", "efg"]

Do not write as if you can get it, but when you are writing a very long time, there will be a lot of useless results, watching all headaches.

=============
Add a sentence, you ask these questions, you put the console out, add the?: and the results of the output once did not know.

  • 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.