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