Regular expression rules in Javascript and C #
This article introduces the knowledge of "regular expression rules in Javascript and C#". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, the concept of regular expression:
In computer science, a single string used to describe or match a series of strings that conform to a syntactic rule. In many text editors or other tools, regular expressions are often used to retrieve and / or replace text content that matches a pattern. Many programming languages support string manipulation using regular expressions.
Second, the use of regular expressions:
In ASP.NET, regular expressions are mainly used to validate the input. There are generally two types of verification: client-side JS verification and server-side verification.
1. JS validates the input
The copy code is as follows:
Function check () {
Var match = / ^\ dbuyer /
Var val = $("# txt") .val ()
If (match.test (val)) {
Alert ("Test passed")
}
Else {
Alert ("Test failed")
}
}
2. C # for verification format
The copy code is as follows:
Protected void Button1_Click (object sender, EventArgs e)
{
String pattern = @ "\ d"
If (System.Text.RegularExpressions.Regex.IsMatch (this.TextBox1.Text, pattern))
{
ClientScript.RegisterClientScriptBlock (GetType (), "", "alert ('successful verification!')", true)
}
Else
{
ClientScript.RegisterClientScriptBlock (GetType (), "", "alert ('verification failed!')", true)
}
}
III. Regular expression rules
\: Mark the next character as a special character, or a literal character, or a backward reference, or an octal escape character; for example,'n 'matches the character "n".' \ n' matches a newline character. The sequence'\ 'matches "\" and "(" matches "(".
^: matches the starting position of the input string.
$: matches the end of the input string.
*: matches the previous subexpression zero or more times. For example, zo* can match "z" and "zoo". * is equivalent to {0,}.
+: matches the previous subexpression one or more times. For example, 'zo+' can match "zo" and "zoo", but not "z". + is equivalent to {1,}.
Matches the previous subexpression zero or once. For example, "do (es)?" Can match "do" in "do" or "does". ? It is equivalent to {0jue 1}.
{n}: n is a non-negative integer. Match the determined n times. For example,'o {2} 'does not match the' o'in 'Bob', but does match the two o in 'food'.
{n,}: n is a non-negative integer. Match at least n times. For example,'o {2,} 'does not match' o'in 'Bob', but does match all o in 'foooood'. O {1,}'is equivalent to 'oasis'. O {0,}'is equivalent to 'oval'.
{n ~ (m)}: M and n are non-negative integers, where n