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 the function of parentheses in regular expressions

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you what the role of parentheses in regular expressions is, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Let me give you an example:

_ window.onload=function () {var date='2015-8-7pm; var re=/ (\ d +) (-) / g; date.replace (re,function (A1 last a2) a3) {/ * alert (A1); * / alert (a2); / * alert (a3); * /}

In the above example, you need to look at the values of a1mena2 and a3 respectively, and you will find some rules.

View a1Flux 2015-8 separately.

View a2ren 2015 8 separately

Check the a3VR separately--

For example, A1 is equivalent to mother, a2 is the eldest son, and A3 is the youngest son. Each son inherits part of his mother's genes.

A1 is the result of this regular var re=/\ dwords.

A2 is the result of the first parenthesis on the left of var re=/ (\ d +) (-) / g

A2 is the result of the second parenthesis on the left of var re=/ (\ d +) (-) / g

Having said the purpose of so many parameters, let's implement the output: August 7, 2015.

Method 1: operate on your son

_ window.onload=function () {var date='2015-8-7pm; var re=/ (\ d +) (-) / g; var a=date.replace (re,function (A1 ~ (A1 ~ a2) ~ (-) a3) {return a2 _ parallel;}); alert (a);}

Method 2: operate on the mother

_ window.onload=function () {var date='2015-8-7mm; var re=/ (\ d +) (-) / g; var a=date.replace (re,function (A1 meme a2Powera3) {return a1.substring (0recovera1) +';}); alert (a);}

After learning the relationship between the parameters of the callback function of the replace () method in the rule and the parentheses, I instantly feel that the regularity has become more complicated.

What is the difference between square brackets [] and parentheses () in regular expressions?

The most basic meaning: parentheses are seen as a whole in parentheses, square braces are one of the matching parentheses, and curly braces are matched several times.

But adding other characters in parentheses has a different meaning, for example:

{n}

N is a non-negative integer. Match the determined n times. For example, "o {2}" does not match "o" in "Bob", but does match two o. S in "food".

{n,}

N is a non-negative integer. Match at least n times. For example, "o {2,}" does not match "o" in "Bob", but can match all o in "foooood". "o {1,}" is equivalent to "o +". "o {0,}" is equivalent to "o *".

{n,m}

M and n are non-negative integers, where n

[] meaningless, generally used to match a set

such as

[0-9]: find any number from 0 to 9.

[a Murz]: find any character from lowercase a to uppercase Z.

() also matches a collection, but it can also be used as a backreference.

What is the function of parentheses in regular expressions? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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