Regular expressions in JavaScript

Source: Internet
Author: User
Tags constructor regular expression alphanumeric characters

A regular expression is an object that describes a character pattern.

JavaScript's RegExp objects and string objects define methods that use regular expressions to perform powerful pattern matching and text retrieval and substitution functions.

In JavaScript, regular expressions are represented by a RegExp object. Of course, you can use a regexp () constructor to create RegExp objects, or you can use JavaScript A new special syntax added in 1.2 to create the RegExp object. Just as a string literal is defined as a character enclosed in quotation marks, the regular expression literal is also defined as a character that is contained between a pair of slashes (/). 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 parttern. This particular RegExp object matches all strings that end with the letter "s". You can also define an equivalent regular expression by using regexp (), as follows:

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

Whether using a regular expression directly or using a constructor regexp (), it is easy to create a RegExp object. The more difficult task is to use regular expression syntax to describe the pattern of characters. JavaScript is a fairly complete subset of the regular expression syntax for Perl language .

The pattern specification for regular expressions is made up of a series of characters. Most characters, including all alphanumeric characters, describe characters that are matched literally. In this case, the regular expression/java/and all of the containing substring "Java" String. Although the other characters in the regular expression are not matched by literal meaning, they all have special meanings. The regular expression/s$/contains two characters.

The first special character "S" is the literal meaning of matching itself. The second character "$" is a special character that matches the end of the string. So the regular expression/s$/matches the end of the letter "s".
The string.

1. Direct measure character

We have found that in regular expressions all alphabetic characters and numbers are matched by literal meaning to themselves. The regular expression of JavaScript also supports some of the non-, through escape sequences that begin with a backslash (\).

Alphabetic characters. For example, the sequence "\ n" matches a literal newline character in a string. In regular expressions, many punctuation marks have special meanings. Here are the characters and their meanings:

The direct measure character of a regular expression

字符 匹配
________________________________
字母数字字符 自身
\ f 换页符
\ n 换行符
\ r 回车
\ t 制表符
\ v 垂直制表符
\ / 一个 / 直接量
\ \ 一个 \ 直接量
\ . 一个 . 直接量
\ * 一个 * 直接量
\ + 一个 + 直接量
\ ? 一个 ? 直接量
\ | 一个 | 直接量
\ ( 一个 ( 直接量
\ ) 一个 ) 直接量
\ [ 一个 [ 直接量
\ ] 一个 ] 直接量
\ { 一个 { 直接量
\ } 一个 } 直接量
\ XXX 由十进制数 XXX 指 定的ASCII码字符
\ Xnn 由十六进制数 nn 指定的ASCII码字符
\ cX 控制字符^X. 例如, \cI等价于 \t, \cJ等价于 \n

___________________________________________________

If you want to use special punctuation marks in regular expressions, you must precede them with a "\".

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.