JS Regular expressions from entry to the ground (3)--scope class

Source: Internet
Author: User
Scope class

When using regular expressions, many times, we would like to match all the letters of A~Z, many people think that we can use the character class [abcdefg...z] , but this method needs to enter all the letters that need to be matched. So, is there a way to do something simple?

Fortunately, regular expressions provide scope classes, which let us use [a-z] to concatenate two characters representing any character from A to Z.

Basic usage
let text = ‘a1b2d3x4z5‘let reg = /[a-z]/gtext.replace(reg, ‘Q‘)  // Q1Q2Q3Q4Q5

Tips: It is worth noting that the range class is a closed interval, that is: [a-z] contains A and Z

Ligatures of the Range class

There is a small trick to using the scope class: [] inside a class you can ligatures, for example:[a-zA-Z]

let text = ‘a1B2d3X4Z5‘let reg = /[a-zA-Z]/gtext.replace(reg, ‘Q‘)  // Q1Q2Q3Q4Q5

JS Regular expressions from entry to the ground (3)--scope class

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.