Example analysis of two regular replacement functions implemented by C#+JS

Source: Internet
Author: User
Tags first string
This article mainly introduces JS and C # Implementation of the two regular replacement function, combined with a specific example of JS and C # for string regular replacement of the relevant implementation methods and considerations, the need for friends can refer to the following

The examples in this paper describe the two regular substitution functions of JS and C # implementations. Share to everyone for your reference, as follows:

application Example 1:

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

Requirements: Change the value after display= to localhost

JS Processing method:

Str.replace (/display=\w*/g, "display=localhost");

C # Processing methods:

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 changed to display=localhosttest name=mu display=localhosttemp

JS Processing method:

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

C # Processing methods:

<summary>///defining the processing method///</summary>///<param name= "Match" > Conforming string </param>///<returns ></returns>private string Evaluator (match match) {  //(display=) (\w*) groups by the found string and then grouped by group  // The No. 0 Group is the entire conforming string, followed by the group of  string str =match in parentheses. 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 summary of JS's regular:

match exec The difference between a string and a regular object

1, when the regular expression is not/g, both 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 the matching string group and ignores grouping, and exec returns the first string or string group

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.