[Code] Regular expressions for C # and JS

Source: Internet
Author: User

Metacharacters

Single character:. []

Qualifier: * +? {n} {N,} {n,m}

Tail: ^ $ '

Other: | ()

Regular expressions are available in both JS and C #.

  

In C #:

Create:

var regex = new Regex ("\\d");

  

The:

Syntax: BOOL IsMatch (String input)

var B = regex. IsMatch (str);

Replace:

Syntax: static string Replace (string input, string pattern, string replacement);

var str = regex.replace (str, "\\d", "number");

Extraction:

Syntax: MatchCollection Matches (String input)

var mts = regex. Matches (str);

    

===========================================================================

JS in:

Create:

var regex = new RegExp (' \\d '); \ \ Need to escape note: I,m,g for ignoring case, matching multiple lines, global matching

var regex =/\d/; Literal methods do not need to be escaped

The:

Boolean regexp.test (String);

var str = ' A1B1C1 ';  var regex =/\d/;    Alert (Regex.test (str)); Popup true, match to number

Replace:

var str = ' A1B1C1 '; Str.replace (/(\d). * (\d)/, ' $2,$1 ') results: a1,1 $ and $ $ represent the first and second results matched to each other.

Extraction:

Objs regexp.exec (str) returns an array-like object

var str = ' A1B1C1 ';  var regex=/\d/;  var objs = regex.exec (str); Result: objs[0] = 1

[Code] Regular expressions for C # and JS

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.