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:
- What is a regular expression?
- Getting started with regular expressions: match a Fixed Single Character
- Getting started with regular expressions: matching any single character
- Getting started with regular expressions: Use character groups
- Getting started with regular expressions: Use character ranges in character groups
- Getting started with regular expressions: Use of assense character groups
- Getting started with regular expressions: matching null characters
- Getting started with regular expressions: Match one or more characters
- Regular Expression: matches zero or multiple characters.
- Regular Expression entry: matches zero or one string.
- Getting started with regular expressions: Match fixed numbers of Characters
- Getting started with regular expressions: match the number of characters in a range
- Getting started with regular expressions: greedy matching
- Getting started with regular expressions: inert matching
- Entry to Regular Expressions: two matching Modes
- Getting started with regular expressions: match word boundaries
- Getting started with regular expressions: boundary definition and relativity
- Getting started with regular expressions: Match non-word boundaries
- Getting started with regular expressions: match the beginning and end of a text
- Entry to regular expression: submode
- Regular Expression entry: "or" Match
- Getting started with regular expressions: replacing with referenced text
- Getting started with regular expressions: unmatched
- Regular Expression Summary: Regular Expressions in JavaScript
- Regular Expression Summary: advanced application of regular expressions in js