Haha, let’s talk a little bit first. Last year, I used C# to make a small syntax highlighting thing. According to the information in the configuration file, the given code is formatted into HTML, so that it can display the same syntax element highlighting effect as in the editor on the web page and support code folding. That's right, it's similar to what you saw in the blog park. Because I was using MSN Space at that time, it did not provide this function, so I had to write one by myself.
I used C# for writing. At first I used super cumbersome basic statements such as for, while, switch, if, etc. to judge keywords, etc. Don't laugh at me. I was stupid and didn't know what regular expressions were at that time, so I could only use this kind of method. Of course, the method is still effective. It's just a long code in a function. It may be very difficult to maintain it in the future. I thought that other software could not be written like this, so I searched on Google for a while, found some syntax-highlighted code and open source projects, and opened it to take a look. . . . . Dizzy, each one is so complicated. To be honest, what I don’t like to do the most is to read other people’s code. It’s not that I am pretentious, but it’s really dizzy to read other people’s code. Unless there is a very detailed document description, I wouldn’t want to read it. At most, I would look at it and see how other people’s interfaces were written, and then guess how they implemented it internally.
Although the search was not very helpful, I still let me know the regular expression and forgot where I saw it. At that time, I began to study regular expressions while modifying my "broken things". Then I reopened my blog in the blog park and finally started using the syntax highlighting function of the blog park. So I wrote a code to highlight the HTML and lost my main motivation. Secondly, the syntax highlighting module made in C# can only run on the server side, or on WinForm programs, and what I end up getting is HTML code to display on the page. I think client scripts are the best for this job. It's a pity that I don't know much about JS. . . Later, during this period, I went around and did not improve the syntax and highlight the module.
Yesterday, I worked overtime and returned home at night. I originally planned to continue learning UML to see the model, but later I remembered that the company had a module that needed to remove all HTML tags in the database returned results, so I opened the RegexBuddy tool RegexBuddy. As a result, when I saw the simple teaching of JScript using regular expressions in the help document of RegexBuddy, I became curious again. I opened UltraEdit-32 and started writing simple JavaScript experiments.
I won’t repeat the nonsense here, because many places are repeated and repeated trials and detours. Here I will give you the usage of regularity in JScript summarized by the experiment.
After the nonsense, let’s get to the topic!
The Prime Minister talks about JScript's regular expression object RegExp.
The class name in JScript that provides regular expression operations is RegExp. There are two ways to instantiate objects of type RegExp.
Method 1, constructor instantiation:
var myRegex = new RegExp("\\w+", "igm ");
//\w+ is the actual regular expression. Note that the first \ is used for escape. Igm means ignoring case, global search, and multi-line search respectively. This will be explained later.
Method 2, direct assignment method:
var myRegex = /\w+/igm;
//The effect is the same as the previous statement, but there is no need to use transfer characters here. What is the original regular expression like? Igm is the same as the igm function in the previous example.
It depends on what method you like. I personally think that the second method is written in a regular way is easier to read. RegexBuddy help document is also recommended. The RegExp object contains the following operations:
exec(string str): executes regular expression matching and returns the matching result. According to the example run results given by MSDN, each execution of exec starts from the last direct match end position, and the returned value seems to be a RerExp object. The explanation given by RegexBuddy is to return an array, but no detailed examples are given. I think it is more reliable based on the experimental results.
compile(string regex, string flags): Precompile regular expressions to make them run faster. After testing, the efficiency has been significantly improved after precompilation. The regex parameter is a regular expression, and flags can be a combination of the following 3 values: g – global search. My experiment result is that without adding the g flag, it can only match the first string that meets the conditions. i – Ignore case m – Multi-line search, it seems that by default it is a multi-line search.
test(string str): If str matches the regular expression, return true, otherwise return false, this match method similar to string object
The RegExp object contains the following properties:
index: The position of the first matching expression in the string, initially at -1
input: The matching target of the regular expression, note that it is read-only
LastIndex: The position of the next matching expression, the original words are (Returns the character position where the next match begins in a searched string.) I don’t know if there is any translation error. I didn’t use this property.
lastMatch: The last string matching the expression
lastParen: The last matching submatch string, for example, there are multiple matches grouped by () in a regular expression. LastParen represents the result of the last group matched
leftContext: All characters from the beginning of the target string to the start of the last match.
rightContext: All characters from the end position of last match to the end position of the entire target string.
$1…$9: represents the result of the nth group matching. This is useful when there are multiple groups in regular expressions ()
Next, let’s talk about the operations related to regular expressions in JScript:
match(string regex): accepts a regular expression and returns whether the string matches this expression.
replace(string regex, string str): Replace the substring that matches the regular expression with str. This function seems simple, but it also hides more advanced usage. Please see the following example.
Example 1:
var str1 = "A:My name is Peter!\nB:Hi Peter!";
str1 = (/Peter/g,"Jack");
alert(str1);
This example is very simple to replace the string. Of course, the power of this expression is not only that. If you are proficient in using it, you can also use it to complete a lot of work that used to require a lot of code. For example, add HTML tags due to highlighting before and after the code keyword. From the previous example, it seems that replace can only replace the matching text with new text. How can you use it to insert labels before and after the keyword? Come back and imagine that if you can use the matching result when replacing, then things will be easy. Just replace the keyword with: tag head + keyword + tag end.
But how do you use the result of regular expression matching in replace?
At this time, we need to use "match variables". Match variables are used to represent the result of regular matches. The following is an explanation of the matching variables:
$& -- represents the result of matching all matching groups. Finally, the matching group is the () grouping of regular expressions.
$$ -- represents $character, because the matching variable uses $character, so it needs to be escaped.
$n-- Similar to the previous $1…$9, indicating the result of the n-th group match.
$nn - It's very simple the result of the nn-th group matching
$`--- It is the leftContext mentioned above. For example, if abcdefg is matched with d, then abc is its leftContext.
$' -- If it matches the above, don't read it wrong! , This is rightContext. To learn from one example, efg is the rightContext of the above example. So now it is very simple to insert labels before and after the keyword:
var str1 = "A:My name is Peter!\nB:Hi Peter!";
str1 = (/Peter/g, "<b>$&</b>");
alert(str1);
It's all 0:39. . . That’s all for writing.
Regular tool software download (password: regex): regex buddy 2.
Please see the example I wrote: JScript syntax highlighting (code simplified)
Here are some examples of MSDN copying:
function matchDemo()
{
var s;
var re = new RegExp("d(b+)(d)","ig");
var str = "cdbBdbsbdbdz";
var arr = (str);
s = "$1 contains: " + RegExp.$1 + "\n";
s += "$2 contains: " + RegExp.$2 + "\n";
s += "$3 contains: " + RegExp.$3;
return(s);
}
function RegExpTest()
{
var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())
if (ver >= 5.5){
var src = "The rain in Spain falls mainly in the plain.";
var re = /\w+/g;
var arr;
while ((arr = (src)) != null)
print( + "-" + + "\t" + arr);
}
else{
alert("You need a newer version of JScript for this to work");
}
}
function matchDemo()
{
var s; //Declare variable.
var re = new RegExp("d(b+)(d)","ig"); //Regular expression pattern.
var str = "cdbBdbsbdbdz"; //String to be searched.
var arr = (str); //Perform the search.
s = "$1 returns: " + RegExp.$1 + "\n";
s += "$2 returns: " + RegExp.$2 + "\n";
s += "$3 returns: " + RegExp.$3 + "\n";
s += "input returns : " + + "\n";
s += "lastMatch returns: " + + "\n";
s += "leftContext returns: " + + "\n";
s += "rightContext returns: " + + "\n";
s += "lastParen returns: " + + "\n";
return(s); //Return results.
}
(matchDemo());
If you pass by, if you have any opinions on this article, please feel free to put forward it here. Let us learn together and make progress together.
I used C# for writing. At first I used super cumbersome basic statements such as for, while, switch, if, etc. to judge keywords, etc. Don't laugh at me. I was stupid and didn't know what regular expressions were at that time, so I could only use this kind of method. Of course, the method is still effective. It's just a long code in a function. It may be very difficult to maintain it in the future. I thought that other software could not be written like this, so I searched on Google for a while, found some syntax-highlighted code and open source projects, and opened it to take a look. . . . . Dizzy, each one is so complicated. To be honest, what I don’t like to do the most is to read other people’s code. It’s not that I am pretentious, but it’s really dizzy to read other people’s code. Unless there is a very detailed document description, I wouldn’t want to read it. At most, I would look at it and see how other people’s interfaces were written, and then guess how they implemented it internally.
Although the search was not very helpful, I still let me know the regular expression and forgot where I saw it. At that time, I began to study regular expressions while modifying my "broken things". Then I reopened my blog in the blog park and finally started using the syntax highlighting function of the blog park. So I wrote a code to highlight the HTML and lost my main motivation. Secondly, the syntax highlighting module made in C# can only run on the server side, or on WinForm programs, and what I end up getting is HTML code to display on the page. I think client scripts are the best for this job. It's a pity that I don't know much about JS. . . Later, during this period, I went around and did not improve the syntax and highlight the module.
Yesterday, I worked overtime and returned home at night. I originally planned to continue learning UML to see the model, but later I remembered that the company had a module that needed to remove all HTML tags in the database returned results, so I opened the RegexBuddy tool RegexBuddy. As a result, when I saw the simple teaching of JScript using regular expressions in the help document of RegexBuddy, I became curious again. I opened UltraEdit-32 and started writing simple JavaScript experiments.
I won’t repeat the nonsense here, because many places are repeated and repeated trials and detours. Here I will give you the usage of regularity in JScript summarized by the experiment.
After the nonsense, let’s get to the topic!
The Prime Minister talks about JScript's regular expression object RegExp.
The class name in JScript that provides regular expression operations is RegExp. There are two ways to instantiate objects of type RegExp.
Method 1, constructor instantiation:
var myRegex = new RegExp("\\w+", "igm ");
//\w+ is the actual regular expression. Note that the first \ is used for escape. Igm means ignoring case, global search, and multi-line search respectively. This will be explained later.
Method 2, direct assignment method:
var myRegex = /\w+/igm;
//The effect is the same as the previous statement, but there is no need to use transfer characters here. What is the original regular expression like? Igm is the same as the igm function in the previous example.
It depends on what method you like. I personally think that the second method is written in a regular way is easier to read. RegexBuddy help document is also recommended. The RegExp object contains the following operations:
exec(string str): executes regular expression matching and returns the matching result. According to the example run results given by MSDN, each execution of exec starts from the last direct match end position, and the returned value seems to be a RerExp object. The explanation given by RegexBuddy is to return an array, but no detailed examples are given. I think it is more reliable based on the experimental results.
compile(string regex, string flags): Precompile regular expressions to make them run faster. After testing, the efficiency has been significantly improved after precompilation. The regex parameter is a regular expression, and flags can be a combination of the following 3 values: g – global search. My experiment result is that without adding the g flag, it can only match the first string that meets the conditions. i – Ignore case m – Multi-line search, it seems that by default it is a multi-line search.
test(string str): If str matches the regular expression, return true, otherwise return false, this match method similar to string object
The RegExp object contains the following properties:
index: The position of the first matching expression in the string, initially at -1
input: The matching target of the regular expression, note that it is read-only
LastIndex: The position of the next matching expression, the original words are (Returns the character position where the next match begins in a searched string.) I don’t know if there is any translation error. I didn’t use this property.
lastMatch: The last string matching the expression
lastParen: The last matching submatch string, for example, there are multiple matches grouped by () in a regular expression. LastParen represents the result of the last group matched
leftContext: All characters from the beginning of the target string to the start of the last match.
rightContext: All characters from the end position of last match to the end position of the entire target string.
$1…$9: represents the result of the nth group matching. This is useful when there are multiple groups in regular expressions ()
Next, let’s talk about the operations related to regular expressions in JScript:
match(string regex): accepts a regular expression and returns whether the string matches this expression.
replace(string regex, string str): Replace the substring that matches the regular expression with str. This function seems simple, but it also hides more advanced usage. Please see the following example.
Example 1:
var str1 = "A:My name is Peter!\nB:Hi Peter!";
str1 = (/Peter/g,"Jack");
alert(str1);
This example is very simple to replace the string. Of course, the power of this expression is not only that. If you are proficient in using it, you can also use it to complete a lot of work that used to require a lot of code. For example, add HTML tags due to highlighting before and after the code keyword. From the previous example, it seems that replace can only replace the matching text with new text. How can you use it to insert labels before and after the keyword? Come back and imagine that if you can use the matching result when replacing, then things will be easy. Just replace the keyword with: tag head + keyword + tag end.
But how do you use the result of regular expression matching in replace?
At this time, we need to use "match variables". Match variables are used to represent the result of regular matches. The following is an explanation of the matching variables:
$& -- represents the result of matching all matching groups. Finally, the matching group is the () grouping of regular expressions.
$$ -- represents $character, because the matching variable uses $character, so it needs to be escaped.
$n-- Similar to the previous $1…$9, indicating the result of the n-th group match.
$nn - It's very simple the result of the nn-th group matching
$`--- It is the leftContext mentioned above. For example, if abcdefg is matched with d, then abc is its leftContext.
$' -- If it matches the above, don't read it wrong! , This is rightContext. To learn from one example, efg is the rightContext of the above example. So now it is very simple to insert labels before and after the keyword:
var str1 = "A:My name is Peter!\nB:Hi Peter!";
str1 = (/Peter/g, "<b>$&</b>");
alert(str1);
It's all 0:39. . . That’s all for writing.
Regular tool software download (password: regex): regex buddy 2.
Please see the example I wrote: JScript syntax highlighting (code simplified)
Here are some examples of MSDN copying:
function matchDemo()
{
var s;
var re = new RegExp("d(b+)(d)","ig");
var str = "cdbBdbsbdbdz";
var arr = (str);
s = "$1 contains: " + RegExp.$1 + "\n";
s += "$2 contains: " + RegExp.$2 + "\n";
s += "$3 contains: " + RegExp.$3;
return(s);
}
function RegExpTest()
{
var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())
if (ver >= 5.5){
var src = "The rain in Spain falls mainly in the plain.";
var re = /\w+/g;
var arr;
while ((arr = (src)) != null)
print( + "-" + + "\t" + arr);
}
else{
alert("You need a newer version of JScript for this to work");
}
}
function matchDemo()
{
var s; //Declare variable.
var re = new RegExp("d(b+)(d)","ig"); //Regular expression pattern.
var str = "cdbBdbsbdbdz"; //String to be searched.
var arr = (str); //Perform the search.
s = "$1 returns: " + RegExp.$1 + "\n";
s += "$2 returns: " + RegExp.$2 + "\n";
s += "$3 returns: " + RegExp.$3 + "\n";
s += "input returns : " + + "\n";
s += "lastMatch returns: " + + "\n";
s += "leftContext returns: " + + "\n";
s += "rightContext returns: " + + "\n";
s += "lastParen returns: " + + "\n";
return(s); //Return results.
}
(matchDemo());
If you pass by, if you have any opinions on this article, please feel free to put forward it here. Let us learn together and make progress together.