In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to apply regular expressions in JavaScript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to apply regular expressions in JavaScript.
Regular expression (regular expression) objects contain a regular expression pattern (pattern). It has attributes (properties) and methods (methods) that use regular expression patterns to match or replace specific characters (or sets of characters) in a string (string). To add attributes to a single regular expression, you can use the regular expression constructor (constructor function), whenever the preset regular expression that is called has a static attribute (the predefined RegExp object has static properties that are set whenever any regular expression is used)
Create:
A text format or regular expression constructor
Text format: / pattern/flags
Regular expression constructor: new RegExp ("pattern" [, "flags"])
Parameter description:
Pattern-- A regular expression text
Flags-if it exists, it will be the following value:
G: global match
I: ignore case
Gi: the above combination
[note] arguments in text format do not need quotation marks, while arguments in the constructor need quotation marks. For example, / ab+c/i new RegExp ("ab+c", "I") implements the same function. In the constructor, some special characters need to be changed (preceded by ""). For example, re = new RegExp ("w +")
Special characters in regular expressions
Character meaning
As a change of meaning, that is, the characters usually followed by "" are not interpreted according to their original meaning, such as / b / matching character "b". When b is preceded by a backslash, it is changed to match the boundary of a word.
-or-
Restore the regular expression function characters, such as "*" matches its preceding metacharacters 0 or more times, / acontest / will match aheroine AAA, after adding "", / aexpressions / will only match "a*".
^ matches the beginning of an input or line, / ^ a / matches "an A" instead of "An a" $matches the end of an input or line, / matches / matches "An a", but does not match "an A" * matches the previous metacharacter 0 or more times, / ba*/ will match the previous metacharacter one or more times, / ba*/ will match the previous metacharacter one or more times? Match the previous metacharacters 0 or 1 times, / ba*/ will match b xyz ba (x) match x save x in a variable named $1... match 9 x | y match x or y {n} exact match n times {n,} more than n times {nMagnem} match NMuir m times [xyz] character set (character set) Match any character (or metacharacter) in this set [^ xyz] does not match any character in this set [] match a backspace match a word boundary B match a word non-boundary cX here, X is a control character, / cM/ match Ctrl-M d matches a character, / d / = / [0-9] / D matches a non-word character / D / = / [^ 0-9] / match a line feed match a carriage return s match a blank character, including, f, v, etc. S match a non-white space character, equal to / [^ f v] / match a tab v match a double straight tab character w match a character that can make up a word (alphanumeric, this is my free translation, including numbers), including underscores For example, [w] matches 5 in "$5.98", which is equal to [a-zA-Z0-9] W matches a character that does not make up a word, such as [W] matches $in "$5.98", which is equal to [^ a-zA-Z0-9].
Having said that, let's look at some examples of the practical application of regular expressions:
E-Mail address Verification:
Function test_email (strEmail) {
Var myReg = / ^ [_ a-z0-9] + @ ([_ a-z0-9] +.) + [a-z0-9] {2jue 3} $/
If (myReg.test (strEmail)) return true
Return false
}
Masking of HTML code
Function mask_HTMLCode (strInput) {
Var myReg = / /
Return strInput.replace (myReg, "")
}
At this point, I believe you have a deeper understanding of "how to apply regular expressions in JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.