JavaScript (c) Regular expressions and the functions implemented

Source: Internet
Author: User

REGEXP is the abbreviation for regular expressions.
Defining the REGEXP regular expression

The RegExp object is used to store the retrieval mode.

Define the RegExp object with the New keyword. The following code defines the RegExp object named P, whose pattern is "e":

var p=new RegExp ("E");  

When you use the RegExp object to retrieve in a string, the character "e" is searched.

Use symbols to describe writing rules:/middle Write regular expression/        ^ match beginning    /^ve/with ve beginning with        $ match end    /ve$/with ve end of        \d     an arbitrary number        \w     An arbitrary number or letter        \s     any string        {n} to repeat the left expression n times        {m,n} repeat the  expression on the left at least m times, up to n times        {m,}  The expression on the left is repeated at least m times, at most not limited to        the left side of the expression, at least one occurrence, at most, the equivalent of {1,}        * The expression on the left, at least 0 times, at most, the equivalent of {0,}        ? The expression on the left, at least 0 times, At most 1 times, the equivalent of {0,1}        [A,b,c]  can only take one of the contents of the square brackets        [A-z] or [1-9] in the scope of the first        | delegate or        ()  priority
var p=new RegExp ("E");   The regular expression is written in the quotation marks in parentheses behind.
_________________________________________________ I'm gorgeous split-line _____________________________________________________
Methods for REGEXP objects

The RegExp object has 3 methods: Test (), exec (), and compile ().

Test ()

The test () method retrieves the specified value in the string. The return value is true or false.

See if there is a wood-like string, there is no true false.

The quotation marks do not contain "E" return False

Contains e reverse return True

exec ()

The EXEC () method retrieves the specified value in the string. The return value is the value that was found. If no match is found, NULL is returned.

See if there is a wood-like string, there is a return rule, no return null

Let him find E.

Let him go find W, no. Return null is not found

compile ()

The compile () method is used to change the REGEXP.

Compile () can either change the retrieval mode or add or remove the second parameter.

Because there is "E" in the string, there is no "D". Back to TrueFalse

_________________________________________________ I'm gorgeous split-line _____________________________________________________
Regular expression to determine whether it is a social security number

<body><script>function func () {varCeshi=NewREGEXP (/^[0-9]{17}[0-9| x]$/); varGuo=document.getelementbyid ("a"). Value; if(Ceshi.test (Guo)) {alert ("correct"); }    Else{alert ("Error"); }}</script>Please enter your ID number<input id="a"Type="text"/><input name="SA"Type="Submit"onclick="func ()"Value="Submit"/></body>

Determine if it is a mailbox

<script>function You () {    varyouxiang=NewREGEXP ("([a-z0-9_\.-]+) @ ([\da-z\.-]+) \. ([A-z\.] {2,6})");varGuo=document.getelementbyid (" You"). Value; if(Youxiang.test (Guo)) {alert ("correct"); }    Else{alert ("Error"); }}</script>Please enter your email<input id=" You"Type="text"/><input name="SA"Type="Submit"onclick="You ()"Value="Submit"/>

JavaScript (c) Regular expressions and the functions implemented

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.