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 realize two regular replacement functions of JS and C#

2025-01-18 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 how to achieve the two regular replacement functions of JS and C#. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The details are as follows:

Application example 1:

Pending string: str= "display=test name=mu display=temp"

Requirement: change all values after display= to localhost

JS processing method:

Str.replace (/ display=\ wigzag, "display=localhost")

C # processing method:

Regex reg=new Regex (@ "display=\ w *"); str=reg.Replace (str, "display=localhost")

Application example 2:

Pending string: str= "display=test name=mu display=temp"

Requirement: string becomes display=localhosttest name=mu display=localhosttemp

JS processing method:

Var reg = / (display=) (\ w*) / GTX var result;while ((result= reg.exec (str))! = null) {str= str.replace (result [0], result [1] + "localhost" + result [2]);}

C # processing method:

/ define the processing method / private string Evaluator (Match match) {/ / (display=) (\ w*) Groups is grouped according to the found strings / / Group 0 is the whole matching string, followed by string str = match.Groups [1] .value + "localhost" + match.Groups [2] .Value; return str } Regex regex = new Regex (@ "(display=) (\ w*)"); string result = regex.Replace (str, Evaluator)

Finally, there is a small regular summary of js:

The difference between string match and regular object exec

1. When the regular expression does not have / g, the two return the first matching string or string group (if there is a grouping in the regular)

2. When the regular expression has / g, match returns all matching string groups and ignores the grouping, while exec returns the first string or string group.

This is the end of this article on "how to achieve two regular replacement functions of JS and C#". 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