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

How to use / g / I / m in javascript regular expression tag

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the javascript regular expression tag / g / I / m how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

First, the js regular logo / gmeme hash iGrample

1 globle Universe g indicates that the expression will be used to find all possible matches in the input string, full-text search for all matching characters that appear, and multiple results can be returned. If you don't add / g, there will only be one match at most.

2 ignorCase I (pencil) indicates that matching is case-insensitive

Ignore case, note that only case is ignored, not half-width.

3Give mutiple means multi-line matching. What is multi-line matching for multi-line search? Is the potential match at both ends of the newline character. Influence the ^ $symbol in the rule

M affects ^, $.

If you do not specify m, then: ^ is only at the beginning of the string and $is only at the end of the string. That is, match the beginning and end of the entire string

If you specify m, then: ^ is at the beginning of each line of the string and $is at the end of each line of the string. That is, match the beginning and end of each line

Second, examples are illustrated.

The usage of 1.

Str = "tankZHang (231144)" + "tank ying (155445)"; res = str.match (/ tank/); / / No / g alert (res) is added; / / display a tank res = str.match (/ tank/g); / / add / g alert (res); / / display as tank,tank

2. The usage of "Zhonqingi".

Str = "tankZHang (231144)" + "tank ying (155445)"; res = str.match (/ zhang/); alert (res); / / displayed as null res = str.match (/ zhang/i); / / added / I alert (res); / / displayed as ZHang

The usage of 3.

Var p = / $/ mg; var s ='1\ N2\ n3\ n4\ n5\ n6; alert (p.test (s)); / / display as true alert (RegExp.rightContext.replace (/\ x0A/g,'\\ a'); / / display\ a2\ a3\ a4\ a5\ a6 alert (RegExp.leftContext); / display as vertical 2345 alert (RegExp.rightContext); / / display as 6 var p = / $/ g Var s ='1\ N2\ n3\ n4\ n6; alert (p.test (s)); / / display as true alert (RegExp.rightContext.replace (/\ x0A/g,'\ a')); / / nothing shows alert (RegExp.leftContext); / / shows vertical 123456 alert (RegExp.rightContext); / / nothing shows var p = / ^ / mg; var s ='1\ N2\ n3\ N4\ N5\ N6' Alert (p.test (s)); / / display as true alert (RegExp.rightContext.replace (/\ x0A/g,'\\ a')); / / display as 1\ a2\ a3\ a4\ a5\ a6 alert (RegExp.leftContext); / / display as vertical 12345 alert (RegExp.rightContext); / / display as 6

/ / as can be seen from the above example, the division of ^ $affected by / m

The three examples mentioned above, respectively, can be used in arrangement and combination. Personally, I don't think / m is of much use.

Supplementary note:

\ nmatches a newline character. Equivalent to\ x0a and\ cJ

RightContext

RegExp.rightContext ($')

This feature is non-standard, please try not to use it in a production environment!

RightContext non-standard attributes are static and read-only attributes of regular expressions, with the most recently matched right substring. RegExp.$' is an alias for this property.

Grammar

RegExp.rightContext

RegExp ["$'"]

Description

The rightContext property is static, not a property of a regular expression independent object. Instead, you should always use it as RegExp.rightContext or RegExp ["$'].

The value of the rightContext property is read-only and is modified when the match succeeds.

You cannot use a property accessor (RegExp.$') to use an abbreviated alias, because the parser sees it here as the beginning of a string and throws a SyntaxError. Use square brackets to access attributes.

Example

Use rightContext and $'

Var re = / hello/g

Re.test ('hello worldview')

RegExp.rightContext; / / "world!"

RegExp ["$'"]; / / "world!"

LeftContext non-standard attributes are static and read-only properties of regular expressions, with the latest matching left substring. RegExp.$ `is an alias for this property.

Grammar

RegExp.leftContext

RegExp ['$']

Description

The leftContext property is static, not a property of a regular expression independent object. Instead, you should always use it as RegExp.leftContext or RegExp ['$'].

The value of the leftContext property is read-only and is modified when the match succeeds.

You cannot use the property accessor (RegExp.$ `) to use the abbreviated alias, because the parser sees it here as the beginning of the template string and throws a SyntaxError. Use square brackets to access attributes.

Example

Use leftContext and $`

Var re = / world/g

Re.test ('hello worldview')

RegExp.leftContext; / / "hello"

RegExp ['$']; / / "hello"

Standard

Non-standard. Is not part of any current specification.

Thank you for reading this article carefully. I hope the article "how to use / g / I / m in javascript regular expression tags" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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.

Share To

Internet Technology

Wechat

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

12
Report