JavaScript Basics -10 JavaScript Regular Expressions (overview, defining Regular, RegExp objects, string methods for pattern matching)

Source: Internet
Author: User
Tags string methods

First, JavaScript Regular expression overview


Regular Expressions Overview

-Regular Expressions (Regular expression): Expressions that are composed of ordinary characters and special characters that describe a particular character rule

-Regular expressions are often used to search, match, or replace specific forms of text in a text. such as: Word frequency statistics, verify whether the string conforms to the message format, block a post of the restrictive words, etc.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/29/wKioL1b4wW_yJn7BAACoqGJARCw283.png "title=" Web.png "alt=" Wkiol1b4ww_yjn7baacoqgjarcw283.png "/>


The initial experience of regular expressions

-Verify that the phone format entered by the user is legal

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/29/wKioL1b4wonxev1RAAG-4aD6pCg216.png "title=" Web.png "alt=" Wkiol1b4wonxev1raag-4ad6pcg216.png "/>



Second, JavaScript defines regular expressions


Normal characters

-All individual uppercase and lowercase letters, numbers are a regular expression that matches a single character, which is the same as itself. Such as:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/29/wKioL1b4xAHD9QQpAACLF7Eesv4072.png "title=" Web.png "alt=" Wkiol1b4xahd9qqpaaclf7eesv4072.png "/>

-some characters in regular expressions have special grammatical meanings and cannot be used directly, and must be escaped with \ to be used

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/29/wKioL1b4xJ3jMoBzAADPJvVUhvM310.png "title=" Web.png "alt=" Wkiol1b4xj3jmobzaadpjvvuhvm310.png "/>


Character

-Regular Expressions match a range of characters using the following syntax

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/2D/wKiom1b4xDDC1igOAAGyJxZvSSM920.png "title=" Web.png "alt=" Wkiom1b4xddc1igoaagyjxzvssm920.png "/>


Pre-defined character set

-The following meta-character references can be used in regular expressions to simplify

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/2D/wKiom1b4xM7jJiU-AAKNbNJEWas192.png "title=" Web.png "alt=" Wkiom1b4xm7jjiu-aaknbnjewas192.png "/>


Quantity words

-A regular expression can use the following special characters to define the frequency of occurrences of a character---quantifier metacharacters

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/29/wKioL1b4xbCCTQsTAAGWNv_vPCo540.png "title=" Web.png "alt=" Wkiol1b4xbcctqstaagwnv_vpco540.png "/>


Select and group

-Regular expressions use the following syntax to define sub-expressions grouping or selecting

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/29/wKioL1b4xtORu_umAAEH6uF4TJI893.png "title=" Web.png "alt=" Wkiol1b4xtoru_umaaeh6uf4tji893.png "/>


Specify a matching location

-You can use the following characters to match a specified position

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/29/wKioL1b4xw3DwJV_AAHWn_tBSl8135.png "title=" Web.png "alt=" Wkiol1b4xw3dwjv_aahwn_tbsl8135.png "/>


Third, JavaScript RegExp object


Create a RegExp object

-There are two methods of creating a regular expression object:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/2A/wKioL1b4yMCwxAguAABWgoEDmHg906.png "title=" Web.png "alt=" Wkiol1b4ymcwxaguaabwgoedmhg906.png "/>


Properties of the REGEXP

-The RegExp object has the following properties:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/2D/wKiom1b4yYrCNX7MAADBzqyT5Hw102.png "title=" Web.png "alt=" Wkiom1b4yyrcnx7maadbzqyt5hw102.png "/>


Methods of REGEXP

-The RegExp object has the following methods

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/2D/wKiom1b4ydPDDPfLAABw_CEx83U746.png "title=" Web.png "alt=" Wkiom1b4ydpddpflaabw_cex83u746.png "/>


Iv. JavaScript string method for pattern matching


Replace method

-Strobject.replace (Substring/regexp,replacement)

-Replaces a substring of a particular format in a string, returning the replaced result

-the first parameter can be either a fixed substring or a regular expression object

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/2E/wKiom1b4137CirctAADPJvMnIjk367.png "title=" Web.png "alt=" Wkiom1b4137circtaadpjvmnijk367.png "/>


Match method

-Strobject.match (SUBSTRING/REGEXP)

-Returns the match of one or more substrings/regular expressions

-Similar to the indexof () method, but it returns the matching value instead of the subscript that matches the string

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/2A/wKioL1b42aeBQi4NAABicTJX24A123.png "title=" Web.png "alt=" Wkiol1b42aebqi4naabictjx24a123.png "/>


Search method

-Strobject.search (RegExp)

-Returns a subscript that matches the specified regular expression string for the first time, and returns 1 if no match is present

-Similar to indexof (), but indexof () does not support regular expressions

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/2E/wKiom1b420zBuMcnAAAxHWECoRQ262.png "title=" Web.png "alt=" Wkiom1b420zbumcnaaaxhwecorq262.png "/>

Split method

-Strobject.split (Str/regexp,[howmany])

-Splits the original string with a specified string or regular expression, returning the array of substrings that were split

-If the Howmany attribute is specified, only the first howmany substring is returned

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/2E/wKiom1b43HiwTWCPAABAmSIyIWk712.png "title=" Web.png "alt=" Wkiom1b43hiwtwcpaabamsiyiwk712.png "/>


Summary: This chapter covers JavaScript regular expressions (overview of regular expressions, defining regular expressions, RegExp objects, string methods for pattern matching)


This article from the "Technical Exchange" blog, declined reprint!

JavaScript Basics -10 JavaScript Regular Expressions (overview, defining Regular, RegExp objects, string methods for pattern matching)

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.