Example of using Java Script regular expressions

Source: Internet
Author: User

First, syntax 1.1 in the use of JS code
var Myregex = new RegExp ("^[-]?[ 0-9][0-9]{0,2}\\. [0-9] {5,15}\\,\s*[-]? [0-9] [0-9] {0,2}\\. [0-9] {5,15}$ "); var Flagmath = Myregex.test (cordinatestring);
1.2 Private characters include: +,*,?

+: Specifies that its leading characters must appear one or more times in the target object.

*: Specifies that its leading character must appear 0 or more times in the target object.

?: Specifies that its leading object must appear 0 or more times in the target object.

{n} matches the determined n times.

{N,} matches at least n times.

{N,m}n <= m, matches at least N times and matches up to M times.

\s: Used to match a single space character, including Tab key and line break;

\s: Used to match all characters except a single space character;

\d: Used to match numbers from 0 to 9;

\w: Used to match letters, numbers, or underscore characters;

\w: Used to match all characters that do not match the \w;

. : Used to match all characters except the line break.

1.3 More commonly used locators include: ^, $, \b,\b

^: The locator specifies that the matching pattern must be at the beginning of the target string

$: locator Specifies that the matching pattern must be at the end of the target object

\b: The locator specifies that the matching pattern must be one of the two boundaries at the beginning or end of the target string

\b: The locator specifies that the matching object must be within the first and end two boundaries of the target string, that is, the matching object is neither the beginning of the target string nor the end of the target string.

Second, the typical application

A regular expression that matches a negative integer:

-[0-9]*[1-9][0-9]*

Regular expressions that match integers:

-?\\d+

Regular expression that matches the nonnegative floating point number (positive floating point + 0):

\\d+ (\\.\\d+)?

Regular expressions that match the positive floating-point numbers:

([0-9]+\\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*))

Regular expression that matches the non-positive floating point number (negative floating point + 0):

((-\\d+ (\\.\\d+)?) | (0+ (\\.0+)?))

Regular expressions that match negative floating-point numbers:

(-([0-9]+\\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*)))

Regular expressions that match floating-point numbers:

(-?\\d+) (\\.\\d+)?

A regular expression that matches 26 English letters to form a string:

[a-za-z]+

A regular expression that matches the uppercase string of 26 English letters:

[a-z]+

A regular expression that matches a lowercase string consisting of 26 26 English letters:

[a-z]+

Regular expressions that match Chinese characters:

[\u4e00-\u9fa5]

Regular expression that matches the limit of the number of characters in Chinese:

[\u4e00-\u9fa5] {2,4}

Regular expressions that match underscores and numbers and letters:

[\w+] {6,12}

Regular expressions that match Chinese characters and letters:

[^\w\x00-\xff] {6,16}

Match two-byte characters (including Chinese characters) to an expression:

[^\x00-\xff]

Regular expression that matches a blank line:

\n[\s|] *\r

Regular expressions that match HTML tags:

/< (. *) >.*<\/\1>|< (. *) \/>/

Regular expression matching the leading and trailing spaces:

(^\s*) | (\s*$) (Trim function like VBScript)

Regular expression that matches the email address:

\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

Regular expression to match URL URL:

http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)?

Regular expression that matches the phone number and starts with a number:

((\ (\d{3}\)) | (\d{3}\-))? 13\d{9}|15[89]\d{8} or [+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +

Match regular telephone, fax number, can "+" start, except number, can contain "-" Regular expression:

\d{3,4}-?\d{7,8}-?\d{0,1} or [+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +

Regular expressions that match a particular form of numbers and identifiers (such as 60~80):

[1-9] [0-9]? [0-9]? ~ [1-9] [0-9]? [0-9]?

Regular expressions that match percentages:

(100| ([1-9]? [0-9]?)) %

Example of using a landline number (JS):

(/^\d{3,4}[-]d{7,8}[-]\d{0,1}$/.test (value))

Example of using Java Script regular expressions

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.