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

Javascript regular expression usage

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

Share

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

This article mainly introduces "javascript regular expression usage". In daily operation, I believe many people have doubts about the usage of javascript regular expression. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "javascript regular expression usage". Next, please follow the editor to study!

Regular expression: syntax: / expression/, slash indicates the starting position

Creation method:

1. Var regObj = new RegExp ("pattern", [, "flags"])

Pattern: required, string of regular expressions

Flags: optional, some combination of flags.

2. Var regObj = / pattern/ [flags]; you cannot enclose pattern and arguments in quotation marks.

Regular representation of the method of an object RegExp object:

Exec: retrieves a match represented by a regular character, returns the value found, and determines its location.

Eg:

Var str = "abcdefg"

Var reg = / cd/

Var bol = reg.exec (str); / / return cd for success and null for failure

Test: retrieves the value specified in the string and returns true or false.

Eg:

Var bol = reg.test (str); / / return true for success and false for failure

Methods of the string object:

Match: find one or more regular representation matches

Search: retrieving values that match regular expressions

Replace: replaces a string that matches a regular expression

Split: separates a string into string numbers; / / the result is an array

Properties of the RegExp object:

Whether the global:RegExp object has the flag gbank / global query. If not, the operation ends after the first match.

Whether the ignorCase:RegExp object has the flag ibank / ignore case

Whether the multiline:RegExp object has the flag m _ bank / multi-line query

Common symbols for regular expressions:

/. /: represents the beginning and end of a pattern

^: match the beginning of the string

$: the end of the matching string

S: any white space character

S: any non-white space character

D: matches a numeric character, level [^ 0-9]

D: any character other than a number is equivalent to [^ 0-9]

W: matches a number, underscore, or alphabetic character, equivalent to [A-Za-z0-9]

W: any non-word character, equivalent to [^ a-zA-Z0-9]

.: any character except a newline character

{n}: match the previous item n times

{n,}: match the previous term n times or multiple times

{nrecoery m}: match the previous item at least n times, but not more than m times

*: match the previous item 0 or more times, equivalent to {0,}

+: match the previous item one or more times, which is equivalent to {1,}

? Match the previous item 0 times or 1 time, which is equivalent to {0jin1}

| |: indicates or; eg: [0-9] | [amurz] |

(): for grouping

Eg:

It's all letters:

Var reg = / ^ [a-zA-Z] + $/; has at least one letter

Four letters:

Var reg = / ^ [a-zA-Z] {4} $/; add'^'to avoid matching things like "3dedf"

11-digit qq number, the first digit cannot be 0:

/ ^ [1-9] {1} d {10} $/

18-digit ID number:

/ ^ d {17} (d {1} | [a-zA-Z} {1}) $/

Mailbox:

/ ^ w {2,} @ w {2,} (. [a-zA-Z] {2Magne 4}) {1pm 2} $/; so that you can match xx@yy.com.cn

At this point, the study on the use of javascript regular expressions is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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