How to use regular expressions in js and C #
This article mainly introduces how to use regular expressions in js and C#, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
Regular expressions in js use the
Function myValid () {var errorMsg = "; var res = true; / / get the value to be validated. Var receiveName = $("# receiveName"). Val (); / name var receiveMobile = $("# tMobile"). Val (); / / Mobile phone number var validCode = $("# validCode"). Val (); / / Verification Code var regName = / ^ [\ u4e00 -\ u9fa5] {2jue 4} $/; / verify name var regMobile = / ^ 1 [3 | 4 | 5 | 7 | 8] [0-9]\ d {8} $/; / verify mobile phone var regCode = / ^\ d {4} $/ / / Verification code if (! regName.test (receiveName)) {errorMsg + = "incorrect name format;\ n\ r"; res = false;} if (! regMobile.test (receiveMobile)) {errorMsg + = "incorrect mobile number format;\ n\ r"; res = false;} if (! regCode.test (validCode)) {errorMsg + = "Please enter a 4-digit numeric verification code;\ n\ r"; res = false } if (! res) {$.ligerDialog.error (errorMsg, "error message");} return res;}
Regular expressions in C # use
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace regular expression {using System.Text.RegularExpressions;// regular expression namespace class Program {static void Main (string [] args) {/ * * regular matching principle: * 1. Only focus on whether there is a string that can be matched, not its location, and other parts that cannot be matched. * 2. Greedy pattern: the regular matches as much as possible, which means it matches to the last string * / while (true) {string str = Console.ReadLine () that can match. The function of the # region metacharacter _ placeholder / / method is to determine whether str matches the custom regular expression rule / / 1.: represents a character placeholder, can represent any character, must pass a character, can be Chinese Console.WriteLine (Regex.IsMatch (str, "b.g")) / / 2. []: for a single character that represents a specified range, you can specify either an intermittent value or a continuous range: 0-9 Amurz legal character: 0-9 Amurz Amurz _ Console.WriteLine (Regex.IsMatch (str, "b [0-9] g")) / / in the rule, ^ can only be written at the beginning. If written in the middle, ^ is an ordinary character Console.WriteLine (Regex.IsMatch (str, "b [0-9A-Z ^ a-zz] g"); Console.WriteLine (Regex.IsMatch (str, "b [^ 0-9A-ZaMuzz ^ g")) / / here [0murz] will contain all the characters between 0murz, as well as some special symbols / / analyzing the reverse order of the "b [9-1] g"-[xmury] range. Console.WriteLine (Regex.IsMatch (str, "b [9-1] g")); Console.WriteLine (Regex.IsMatch (str, "b [0-9 -] g")); / 11-19 Console.WriteLine (Regex.IsMatch (str, "[11-19]")) only in the order of ASCII codes / / 11-19 Console.WriteLine (Regex.IsMatch (str, "b [^ 0-9] g"); / / |: or Console.WriteLine (Regex.IsMatch (str, "[0-9] [0-9] | [0-9] [0-9] [0-9]") / / hjasdgf1234fjhadsjfhas / / if strict digit matching is involved, you need to add the start ^ and the end $: the character must strictly match the regular Console.WriteLine from beginning to end (Regex.IsMatch (str, "^ [0-9] [0-9] $| ^ [0-9] [0-9] $")) / / all the characters contained in [] are ordinary characters, not metacharacters, except for ^-- need to be followed by something. If you need to match, you can consider using escape\ Console.WriteLine (Regex.IsMatch (str, @ "^\ ^ $"); Console.WriteLine (Regex.IsMatch (str, "^\ ^ $")). # endregion # region metacharacter _ modifier / / *: it is not a character placeholder, indicating that it does not represent a character, it is used to modify * the previous subexpression appears 0 or more times: the subexpression refers to the character before * by default, if you need to represent more than one character Use () to include Console.WriteLine (Regex.IsMatch (str, "ab*g")) Console.WriteLine (Regex.IsMatch (str, "^ (ab) * g $")) / / +: it is not a character placeholder, which means that it does not represent a character. It is used to modify the occurrence of one or more times of the subexpression before +: the subexpression refers to the character before + by default. If you need to represent more than one character, use () to include Console.WriteLine (Regex.IsMatch (str, "aquig")). / /?: it is not a character placeholder, which means that it does not represent a character, it is used to modify? The previous subexpression appears 0 or 1 times: does the subexpression refer to by default? The preceding character, if needed to represent more than one, uses () to include Console.WriteLine (Regex.IsMatch (str, "^ asigg$")). / / {NMagne m}: {NMagne m} it is not a character placeholder, which means that it does not represent a character. It is used to modify the subexpression in front of {NMagne m} to appear at least n times and m times at most Console.WriteLine (Regex.IsMatch (str, "^ [0-9] {3jue 4} $")). / / {n}: it is not a character placeholder, which means that it does not represent a character. It is used to modify that the subexpression preceding {n} can only appear Console.WriteLine n times (Regex.IsMatch (str, "^ [1-9] [0-9] {17} $| ^ [0-9] {15} $| ^ [0-9] {17} [xX] $")). / / {n,}: it is not a character placeholder, which means that it does not represent a character. It is used to modify the previous subexpressions of {n,} to appear at least n times, with no restrictions on Console.WriteLine (Regex.IsMatch (str, "^ [0-9] {3,} $"); Console.WriteLine (Regex.IsMatch (str, "^ QQ $")) # endregion / / determine whether the login name contains a special symbol in c #: [^ 0-9A region Zamurz _]: also cannot contain the Chinese # region abbreviation expression / /\ d: represents a number, equivalent to [0-9] Console.WriteLine (Regex.IsMatch (str, @ "^\ d {3,} $")) / /\ D: non-numeric Console.WriteLine (Regex.IsMatch (str, @ "\ D")); / /\ s: blank character: space, tab stop, newline Console.WriteLine (Regex.IsMatch (str, @ "\ s")); / /\ S: non-white space character Console.WriteLine (Regex.IsMatch (str, @ "\ S")) / /\ w: [0-9 a murz Amurz _ Chinese] Console.WriteLine (Regex.IsMatch (str, @ "\ w")); / /\ W: special symbol Console.WriteLine (Regex.IsMatch (str, @ "\ W") other than [0-9 a murz A murz Z _ Chinese]) # endregion} / / determines whether the string is the correct domestic phone number, regardless of the extension. / / 010-8888888 or 010-88888880 or 010xxxxxxx / / 0335-8888888 or 0335-88888888 (area code-phone code) / / 10086,10010,95595,95599,95588 (5 digits) / / 13888888888 (11 digits are all digits) while (true) {string str = Console.ReadLine () / / Don't try to match Console.WriteLine one by one (Regex.IsMatch (str,@ "^\ d {3jue 4} [-]?\ d {7c8} $| ^ [1-9]\ d {4} $| ^ 1 [3-9]\ d {9} $")) } / / verify the validity of the email address entered by the user wuhu0723@126.com while (true) {string str = Console.ReadLine () / / Don't try to do it in one step. Match Console.WriteLine one by one (Regex.IsMatch (str, @ "^ [0-9A Zamurz] + [@] [0-9A ZaMuz] + [.] [A-Za-z] {2jue 5} $")) Thank you for reading this article carefully. I hope the article "how to use regular expressions in js and C#" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!