Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize two regular replacement functions of JS and C#

Shulou Source: shulou.com Published: 2022-06-02 11:41:45 09月10日 Update

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.

Tags: Characters strings regularities processing methods grouping clocking articles two functions examples more expressions applications good practical content objects parentheses articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi Docker MariaDB Huawei