Foreign language translation--javascript Tutorial--regular expression--(2)

Source: Internet
Author: User
Tags alphabetic character character classes

character class

Suppose we want to find a number in the string. Not the specified number, but any number, for example: Find "1" in "Only 1", and look for "5" in "Give me a 5".

Substring matching can be used in loops to find all the numbers in 0-9. However, the use of a regular formula can be handled more gracefully.

Regular can use character classes instead of specific characters.

For example, a regular uses "\d" to represent any number. The following example matches a number:

1 showmatch ("I ' m 5 yearsold",/\d/)   //  5

The most commonly used character classes are:

Any number in the \d:0-9;

\s: whitespace characters, such as tab indentation, line breaks, and so on;

\w: Latin alphabet, numerals or underscores;

A regular may include the character class and the normal tag together:

1 showmatch ("I ' m the 1stOne",/\dst/)   //  matches ' 1st '

The following code has a regular formula that contains several character classes together:

1 showmatch ("I ' M 1",/\d\s\w\w\w\w/)   //  1 year

There are also some antisense character classes:

\d: Non-digital;

\s: non-whitespace characters;

\w: Non-Latin alphabet, numerals or underscores;

For example, find the first non-alphabetic character:

1 showmatch ("I ' M 1year old",/\w/)   //  matches apostrophe '

A regular can also contain non-printable string characters: \ n (line break), \ t (tab), and so on. These are obviously just characters, not character classes.

Space is very important!

Normally, we don't care about spaces. "1-5" and "1-5" look no different.

However, in a regular style, spaces and other identities are the same .

The following regular mode does not work correctly because there is no space identifier included:

1 showmatch ("1-5",/\d-\d/)  //  no matches!

Let's change it. We can put the space mark in the regular formula, or it is best to include the space in it:

1 showmatch ("1-5",/\d-\d/)   //  works2 showmatch ("1-5//  also works3 showmatch ("1-5//   fails! (no spaces in string)

The last match failed because there are no spaces in the example. So don't put extra spaces in the regular, they all make sense.

In the regular style, the point " . " means any character other than the line break:

1 showmatch ("a char//  " char "2 showmatch ("a ch-r// "Ch-r" 3 showmatch ("a ch R//  " Ch R ", the space is also a char

Although the point " . " can represent all characters, it must have characters:

1 showmatch ("A chr// not found

Foreign language translation--javascript Tutorial--regular expression--(2)

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.