JavaScript Regular Expressions Basic Text _ regular expressions

Source: Internet
Author: User
Tags alphabetic character

Regular expression: syntax:/expression/, slash indicates starting position

How to create:

1, var regobj = new RegExp ("pattern", [, "flags"]);
Pattern: Required option, regular expression string;
Flags: Optional, some combinations of flags.
2, var regobj =/pattern/[flags]; This method cannot enclose pattern and flags in quotes.

The method that represents the object RegExp object is regular:

EXEC: Retrieves a regular representation of a match, returns the found value, and determines its position.
eg
var str = "ABCDEFG";
var reg =/cd/;
var bol = reg.exec (str); Successfully returned CD, failed to return null;
Test: Retrieves the value specified in the string, returns True or false.
eg
var bol = reg.test (str); Returns true successfully, failure returns false;

String object's method:

Match: Finds one or more regular representations of matches;
Search: Retrieves a value that matches the regular expression;
Replace: Replaces the string that matches the regular expression;
Split: Separates strings into string numbers;//The result is an array;

Properties of the RegExp object:

Global:regexp object has a flag g;//global query, if not this property, the first match after the operation;
Whether the Ignorcase:regexp object has a flag i;//ignores case;
Whether the Multiline:regexp object has a flag m;//multiple rows query;

Common symbols for regular expressions:

/.../: Represents the beginning and end of a pattern;
^: Match the start of the string;
$: The end of the matching string;
S: any whitespace character;
S: Any non-white-space character;
D: match a digit character, rank [^0-9];
D: Any character other than numbers, equivalent to [^0-9];
W: Matches a number, underscore, or alphabetic character, equivalent to [a-za-z0-9];
W: Any non-word character, equivalent to [^a-za-z0-9];
.: Any character other than a line break;
{n}: matches n times before;
{N,}: matches the previous n times, or multiple times;
{n,m}: matches the previous item at least n times, but not more than m times;
*: Match the previous 0 or more times, equivalent to {0,};
+: Match the previous 1 or more times, equivalent to {1,};
? : Match 0 or 1 times before, equivalent to {0,1};
to express or mean; eg:[0-9]| [A-z];
(): For grouping;

eg

    All letters:
        var reg =/^[a-za-z]+$/; at least one letter;
    Four letters:
        var reg =/^[a-za-z]{4}$/; Plus ' ^ ' avoids ' 3dedf ' This kind of also matches up;
    11-bit QQ number, first digit cannot be 0:
       /^[1-9]{1}d{10}$/
    18-bit identification number:
       /^d{17} (d{1}|[ A-za-z}{1}) $/
    mailbox:
       /^w{2,}@w{2,} (. [ a-za-z]{2,4}) {1,2}$/; so you can match xx@yy.com.cn;

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.