The regexp in JavaScript

Source: Internet
Author: User



Regular expressions are a set of independent rules that almost every programming language provides classes that implement these rules

1. Create a regular object in two ways

The first method of creation:

var reg = new RegExp (pattern,flags);

Second method of Creation (recommended):

var reg =/pattern/flags;//flags If is g, indicates global match, if I, the case is ignored



2. Two common methods

Method Role
Test ()

Tests if a piece of text matches this regular expression

Retrieves the specified value in a string. The return value is true or false.

EXEC ()

You can get a portion of text that matches a regular match (the return value is an array or null)

The return value is the value that was found. If no match is found, NULL is returned.

3. Example


(1) Identify each matching item

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/86/9F/wKioL1fFpR_x78CIAAGpJMuVDgM508.gif "title=" Find each matching item. gif "alt=" Wkiol1ffpr_x78ciaagpjmuvdgm508.gif "/>


(2) Find out all mobile phone numbers

<! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/A0/wKiom1fFpb-R0TwVAAHH9SgaYZc680.gif "title=" Find out all mobile phone numbers. gif "alt=" Wkiom1ffpb-r0twvaahh9sgayzc680.gif "/>


(3) Replace All

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/9F/wKioL1fFpmqyd9i_AAFh8K02vTg533.gif "title=" Replace all. gif "alt=" Wkiol1ffpmqyd9i_aafh8k02vtg533.gif "/>


(4) Verifying the mailbox format

<! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/A0/wKiom1fFqOuB8utEAADQMPHGq-4305.gif "title=" Verify the mailbox format 2.gif "alt=" Wkiom1ffqoub8uteaadqmphgq-4305.gif "/>



The regexp in JavaScript

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.