Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a regular expression in Javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what regular expressions are in Javascript. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Undefined

What is a regular expression?

A regular expression (Regular Expression or Regex) is a combination of characters used to define a particular search pattern. Regular expressions can be used to match, find and replace characters in text, verify input data, find spelling errors in English words, and so on.

Debugging tool

Here are some excellent online debugging tools that you may need if you want to create or debug regular expressions. Personal preference Regex101,regex101 supports switching between different flavor of regular expressions, interpreting your regular expressions, displaying matching information, providing common syntax references, and so on.

Regex101

Regexr

Regexpal

Start

In Javascript, a regular expression begins and ends with /, so as simple as / hello regexp/ is a regular expression.

Flags (marker or modifier)

Flags is written after the ending /, which can affect the matching behavior of the entire regular expression. Common flags includes:

G: global match (global); regular expression returns only the first match result by default, and all matches can be returned by using the marker g: ignore case (case-insensitive); ignore the case of letters m: multi-line match (multiline) when matching Think of the start and end characters (^ and $) as working on multiple lines, that is, matching the beginning and end of each line (split by\ n or\ r), rather than just the beginning and end of the entire input string

Flags can be combined, such as:

Character Sets (character set)

Used to match any character in a character set, common character sets are:

[xyz]: match "x" or "y" ``"z" [^ xyz]: complement set, match other characters except "x"y"z" [a murz]: match any character from "a" to "z" [^ amurn]: complement Match other characters except "a" to "n" [Amurz]: match any character from "A" to "Z" [0-9]: match any number from "0" to "9"

For example, matching all letters and numbers can be written as: / [a-zA-Z0-9] / or / [a-z0-9] / I.

Quantifiers (quantifier)

In practice, we often need to match the same type of characters multiple times, such as matching 11-bit mobile phone numbers. It is impossible for us to write [0-9] 11 times. At this time, we can use Quantifiers to achieve repeated matching.

{n}: match n times {nrecom}: match n times {n,}: match > = n times?: match 0 | once *: match > = 0 times, equivalent to {0,} +: match > = 1 times, equivalent to {1,}

Metacharacters (metacharacters)

There are some letters with special meaning in regular expressions, which are called metacharacters. In short, metacharacters are characters that describe characters, which are used to describe the content, transformation and various operation information of character expressions.

Common metacharacters are:

\ d: matching any number is equivalent to [0-9]\ D: matching any non-numeric character;\ d complement\ w: matching letters and numbers and underscores in any basic Latin alphabet; equivalent to [A-Za-z0-9]\ W: matching letters and numbers in any non-basic Latin alphabet, and underscores \ s: matches a space character, including spaces, tabs, page feeds, line breaks, and other Unicode spaces\ s: matches a non-space character;\ s complement: matches a zero-width word boundary, such as between a letter and a space; for example, /\ bno/ matches "no" in "at noon", / ly\ b / matches "possibly yesterday." "ly"\ B in: matches a zero-wide non-word boundary, such as between two letters or spaces; for example, /\ Bon/ matches "on" in "at noon", / ye\ B / matches "possibly yesterday." "ye"\ t: matches a horizontal tab (tab)\ n: matches a newline character (newline)\ r: matches a carriage return (carriage return)

Special Characters (special characters)

There are some special characters in the rule, which do not match literally, but have a special meaning, such as?, *, + used for quantifiers mentioned earlier. Other common special characters are:

\: escape characters, you can convert ordinary characters to special characters. For example,\ w; you can also convert special characters to literal meaning, such as\ + match "+".: matches any single character, except for newline characters:\ n,\ r,\ u2028 or\ u2029; in the character set ([.]), there is no special meaning, that is, it means'.' Literal meaning of |: replacement character (alternate character), matching the expression before or after. For example, if you need to match both "bear" and "pear", you can use / (b | p) ear/ or / bear | pear/; but not / b | pear/, this expression can only match "b" and "pear" ^: match the beginning of the input. For example, / ^ A / does not match "A" in "an Apple", but matches "A" $: in "An apple" matches the end of the input. For example, / tweak / does not match the "t" in "eater", but matches the "t" in "eat". ^ and $are often used in form validation because you need to validate a complete input from start to end, rather than matching a segment of the input

Groups (xyz): capture packets (Capturing Group), match and capture matches; for example, / (foo) / match and capture "foo bar." "foo" in. The matched substring can be found in the elements [1],..., [n] of the result array, or in the properties of the defined RegExp object $1,..., $9. (?: xyz): non-capture grouping (Non-capturing Group) that matches but does not capture matches The match cannot be accessed again.\ nParse n is a positive integer indicating a back reference pointing to the matching substring in the nth parenthesis (counting from the left) in the regular expression; for example, / apple (,)\ sorange\ 1 / matches "apple, orange, cherry, peach." "apple,orange," in

Assertion (assertion) x (? = y): matches only the x followed by y; for example, / bruce (? = wayne) /, if "bruce" is followed by wayne, match it. / bruce (? = wayne | banner) /, if "bruce" is followed by "wayne" or banner, it matches. However, neither "wayne" nor "banner" will appear x (?! y) in the matching result: only match x that is not followed by y; for example, /\ d + (?!\.) / will only match not to be "." The number to follow.

/\ d + (?!\.) / .exec ('3.141') `matches `141 "` instead of `" 3.141 "

Finally, we recommend Fundebug, an easy-to-use BUG monitoring tool.

Application

The syntax and rules of so many regular expressions listed above can help us analyze and understand the function of a regular expression to a certain extent, but how to combine these rules and create expressions with specific functions requires more practice. Here are a few examples to illustrate the use of these rules.

1. Match the mobile phone number

Let's start with a relatively simple matching mobile phone number. At present, the domestic mobile phone number is an 11-digit number that begins with 1 (3, 4, 5, 5, 7, 8), so the regularity of the mobile phone number can be divided into the following parts:

Start with 1: / ^ 1 / the second digit is one of 3, 4, 5, 7, 8: / [34578] / or / (3 | 4 | 5 | 7 | 8) / the remaining 3-11 digits are numbers and end with a number: /\ d {9} $/

Taken together, it is / ^ 1 [34578]\ d {9} $/ or / ^ 1 (3 | 4 | 5 | 7 | 8)\ d {9} $/. Because of the performance loss caused by the use of capture parentheses, the first method is recommended.

two。 Match email

The standard email group is @.

The format standard for each part is (simplified accordingly, mainly to show how to write rules):

Yourname: any letter (a-z/A-Z), number (0-9), underscore (_), period (.), hyphen (-), length greater than 0domain: any letter (a-z/A-Z), number (0-9), hyphen (-), length greater than 0extension: any letter (a-z/A-Z), length 2-8optional-extension: "." Beginning, followed by any letter (a-z/A-Z), length 2-8, optional

The regular expression for each part is:

Yourname:/ [a murz\ d.colors -] + / domain:/ [a murz\ d -] + / extension:/ [a murz] {2jue 8} / optional-extension:/ (\. [amerz] {2jue 8})? /

Combine to form the final regular expression: / ^ ([amurz\ d.colors -] +) @ ([amurz\ d -] +)\. ([amurz] {2mer8}) (\. [aripz] {2mer8})? $/; for readability, wrap each part in "()" and don't forget the start and Terminator ^ $.

This is the end of this article on "what is regular expression in Javascript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report