Getting started with regular expressions: Use character ranges in character groups

Source: Internet
Author: User
Tags arabic numbers ranges

Now suppose we need to match a group of file names, whose names are city0.jpgw.city1.jpg...city9.jpg. According to the content described above, we can easily write such an expression: “city201701234567892.16.jpg ". That's right! In this way, we can achieve the desired effect. If you think it is not difficult to write 10 Arabic numbers, what if you want to match such a file name? A_1.jpg1_ B _1.jpg1_c_1.jpg..z_1.jpg. This time, your expression becomes like this: zookeeper abcdefghijklmnopqrstuvwxyz1__1.jpg ", wow! It seems uncomfortable.

Regular Expressions provide character ranges to simplify this syntax. The syntax is "Start character-end character ". For the example matching Arabic numbers above, it is written as [0-9].

You do not have to write the match as "[0-9]". You can write it as "[0-3]" as needed. It will only match the range. The starting character and ending character are based on the size of its ASC Ⅱ value, that is, it will match all the characters (including the start and end characters) whose ASC Ⅱ code is located between the start and end characters ). In addition, if the ASC Ⅱ value of the starting character is greater than the ASC Ⅱ value of the ending character, for example, if you write "[3-0]", an error will occur, leading to a matching failure.

Effect demonstration

The regular expression is as follows:

bkjia[0-3]....

Www.bkjia1.net

Www.bkjia2.com

Www.bkjia3.org

Www.bkjia1.net

Www.bkjia2.com

Www.bkjia3.org

This example code
<script type="text/javascript">function reg_replace(){var test = document.getElementById("test");regex = new RegExp("bkjia[0-3]....","g");test.innerHTML = test.innerHTML.replace(regex,"<span style='background-color:orange'>bkjia.xxx</span>");}function reg_split(){var test = document.getElementById("split");regex = new RegExp("bkjia[0-3]....","g");test.innerHTML = test.innerHTML.split(regex);}</script>

If you want to match "-" in a character group ("["] "), you need to use an escape character in the format of"-". in addition to "[" "]", "-" becomes a common character, and no escape is required.

In the same sense, we can write "[a-z]" to match all lower-case letters, "[A-Z]" to match all the upper-case letters, here is no longer an example.

Now let's assume that we need to match all the RGB colors in HTML. We know that in the Web, colors are usually represented as values such as "# FF00CC", so we can combine them with the content described above, we can use matching like this:

BGCOLOR = "#336633", regular expression # [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]

Additional reading

The topic list of this article is as follows:

  1. What is a regular expression?
  2. Getting started with regular expressions: match a Fixed Single Character
  3. Getting started with regular expressions: matching any single character
  4. Getting started with regular expressions: Use character groups
  5. Getting started with regular expressions: Use character ranges in character groups
  6. Getting started with regular expressions: Use of assense character groups
  7. Getting started with regular expressions: matching null characters
  8. Getting started with regular expressions: Match one or more characters
  9. Regular Expression: matches zero or multiple characters.
  10. Regular Expression entry: matches zero or one string.
  11. Getting started with regular expressions: Match fixed numbers of Characters
  12. Getting started with regular expressions: match the number of characters in a range
  13. Getting started with regular expressions: greedy matching
  14. Getting started with regular expressions: inert matching
  15. Entry to Regular Expressions: two matching Modes
  16. Getting started with regular expressions: match word boundaries
  17. Getting started with regular expressions: boundary definition and relativity
  18. Getting started with regular expressions: Match non-word boundaries
  19. Getting started with regular expressions: match the beginning and end of a text
  20. Entry to regular expression: submode
  21. Regular Expression entry: "or" Match
  22. Getting started with regular expressions: replacing with referenced text
  23. Getting started with regular expressions: unmatched
  24. Regular Expression Summary: Regular Expressions in JavaScript
  25. Regular Expression Summary: advanced application of regular expressions in js

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.