Javascript: regular expression (1)

Source: Internet
Author: User

Regular expressions have a lot of content, but this series mainly discusses the most common knowledge points. We all have this experience in our study: if too many knowledge points are discussed each time, it will not be easy to absorb and digest, nor will it be conducive to the formation of the Knowledge context, therefore, this series will divide limited content into several articles for discussion.

A regular expression is an object that describes the character mode. JavascriptRegexpClass indicates a regular expression, while string and Regexp both define functions that use regular expressions for powerful pattern matching and text retrieval and replacement.

Ecmascript v3The Javascript Regular Expression is standardized. Javascript 1.2 implements a subset of the regular expression features required by ecmascript V3, and JavaScript 1.5 implements the complete standard. The regular expressions in JavaScript are fully based on the regular expression tool in Perl. Roughly speaking, JavaScript 1.2 implements the Regular Expression of Perl 4, and JavaScript 1.5 implements a large subset of the Regular Expression of Perl 5.

① Definition of a regular expression:

In JavaScript, regular expressions are represented by Regexp objects. You can use Regexp () to create a Regexp object.Direct amount syntaxTo create a Regexp object. Just as the string is defined as a character that contains quotation marks,A regular expression is defined as a character that contains a slash.Therefore, JavaScript may contain the following code:

var pattern=/s$/;

This line of code creates a new Regexp object and assigns it to the variable pattern. This special Regexp object matches all strings ending with the letter "S.

alert(/s$/.test("apples"));  //true

You can also use the Regexp () constructor to define an equivalent regular expression. The Code is as follows:

var pattern=new RegExp("s$");

It is easy to create a Regexp object no matter whether you use the direct syntax or the constructor syntax.

The pattern specification of a regular expression is composed of a series of characters.Most characters (including all letters and numbers) Describe the matching characters based on the direct quantity. In this way, the regular expression/Java/matches all strings containing the sub-string "Java.

alert(/java/.test("abcjavacba"));

However, other characters in a regular expression do not match the Regular Expression by quantity. They all have special meanings. For example, the regular expression/S $/contains two characters. The first character "S" matches itself according to the direct quantity. The second character "$" is a special metacharacter, it matches the end of the string. Therefore, the regular expression/S $/matches a string ending with the letter "S.

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.