Common regular expressions and applications of JavaScript (I.)

Source: Internet
Author: User

Both the string class and the RegExp object class of JavaScript define methods that use regular expressions for pattern matching, and this article will introduce the common regular expressions and related applications of JavaScript in a serialized way, and welcome the communication

This section is a serial one that first introduces the regular expressions commonly used in the JavaScript language, and how the application will be introduced in the latter few.

All cases will be tested using the online regular expression validation tool REGEXR, the address is: http://regexr.com/v1/, this section of the test is from the site.

1. Match string literals:

Regular expression literals are contained within a pair of forward slashes, such as Var parttern=/p/, which means matching the uppercase character "P" (using the REGEXR test without entering a forward slash, directly entering the literal value in the Match field)

As shown, enter "P" in the Match field (both global and multiline, meaning will be explained below), highlighting all uppercase "P" in the target document in blue-green:

Entering any character or number that appears in this article will result in a direct match, and you will be interested in trying again.

2. Matching character set and character set shorthand

If you want to match a certain type of character in the target text, such as a number, a space, or a lowercase letter, use a character set, and the following is a common character set regular expression and its shorthand form:

For the test number matching instance, [0-9] and the \d match the same result:

In addition, you can use the dot "." In Regexr. Match any character, see:

3. Match Meta-character literals

Some character input literals in regular expressions are not matched, which are metacharacters. Metacharacters has the following types: ^ $. * + ? = ! : | \/() []{} each meta-character has its special meaning, as explained below. Meta-character literals do not participate in matching, and there are two common ways to match meta-character content:

1) The meta-character is escaped with a backslash "\" and the following example uses "\." To match the dot number "." Literal value:

2) Place the metacharacters between \q and \e, and the following example uses "\q.\e" to match the dot number "." Literal value:

4. quantifiers for regular expressions

In section 2 above, we tick global in REGEXR and use \d to match all the numbers that appear in the target text. All numbers are highlighted.

If we want to match the target text of the phone number tel. (089) 387109-0, how to deal with it? This phone number consists of a () 3-digit area code, 5 digits, a connector, and a 1-digit number. You can use the following syntax to match phone numbers:

\ (\d{3}\) \d{6}[-]\d{1} (? =\;)

The following is an article-by-article explanation of the syntax:

The (? =\;) is used to ensure that only the phone number is matched. If you want to match both the phone number and the fax number, you need to use the following formula:

\ (\d{3}\) \d{6}[-]\d{1,2}

Where d{1,2} indicates that it can match at least 1 digits, up to 2 digits. By visible, the expression can match both the phone number and fax number of the target document:

Regular expression quantifiers In addition to the \d{n} and \d{n,m} used above, there is a question mark "?", an asterisk "*" and a plus "+", respectively, with different meanings:

Common regular expressions and applications of JavaScript (I.)

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.