It should be noted that no matter whether it is simple or complex, the above description only matches a single character. If you need to match a long string, every character that makes up this string is complicated (not abbreviated as \ d). You can imagine how complicated an expression is.
Let's review the example of matching colors in the Web. Our regular expression is written like this: "# [0-9a-f] [0-9a-f] [0-9a-f] [0-9a-f] [0-9a-f] [0-9a-f] [0-9a-f]". It should be thought of, if there is a way to determine the number of matching individual characters. Here we will discuss how to use regular expressions to match multiple characters.
In a regular expression, it can be in a single character (such as "j"), a character group (such as "[abcde]"), or a specific character type (such as "\ d "), any character (that is, ". ") followed by" + "to represent a string that matches one or more (in other words, at least one) characters.
For the example above, for "n +", there are:
Effect demonstration
The regular expression is as follows:
n+
Www.bkjia.com
Www.google.com
Www.bkjia.mmc.org
This example code
function reg_replace(){var test = document.getElementById("test");aim = "n+";var regex = new RegExp("("+aim+")","g");test.innerHTML = test.innerHTML.replace(regex,"<span style='background-color:orange'>$1</span>");}
We now consider a practical example of my email writing: bkjia@163.com, if there is no knowledge in this chapter, it is impossible to match the email address, because, we cannot determine the length of the email address. Now, if we want to match this email address and combine the previous knowledge with the new content in this section, it is easy to get this expression: \ w + @ (\ w + \\.) + [a-z] {2, 3}
Bkjia@163.com
Bkjia@google.com
Www. bkjia @ org
Www@bkjia.org
As mentioned earlier, "-" is only a metacharacter within the "[]" range of the character group and is a common character other. Here, "." is a metacharacter beyond the "[]" range, used to match any single character. Within the "[]" range, it becomes a common character. Therefore, "[\ w \.]" can be abbreviated as "[\ w.]" ("[\ w \.] will not cause errors ").
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