Basic knowledge of Regular Expressions in JavaScript programming

Source: Internet
Author: User
This article mainly introduces how to use regular expressions in JavaScript programming and lists the oxo Regular Expressions supported by JS. For more information, see RegExp: it is short for a regular expression (regular expression.
What is RegExp?
Regular expressions describe character pattern objects.
When you retrieve a text, you can use a mode to describe the content to be retrieved. RegExp is in this mode.
A simple mode can be a single character.
More complex modes include more characters and can be used for parsing, format check, replacement, and so on.
You can specify the search position in the string and the type of characters to be retrieved.
Syntax

var patt=new RegExp(pattern,modifiers);

Or

var patt=/pattern/modifiers;

Mode describes an expression model.
Modifier describes whether the search is global and case sensitive.
RegExp Modifier
Modifiers are used to perform case-insensitive and full-text searches.

  • The I-modifier is used to perform case-insensitive matching.
  • The g-modifier is used to perform a full-text search (instead of stopping the search when the first one is found, but finding all the matches ).

Instance 1

Find "W3CSchool" in strings case-insensitive"

var str="Visit W3CSchool";var patt1=/w3cschool/i;

The text marked below is a matched expression:

Visit W3CSchool

Instance 2
Full-text search "is"

var str="Is this all there is?";var patt1=/is/g;

The text marked below is a matched expression:

Is this all there is?


Instance 3
Full-text search and case-insensitive search "is"

var str="Is this all there is?";var patt1=/is/gi;

The text marked below is a matched expression:

Is this all there is?

Next, let's take a look at some of the basic regular expression objects available in JS:
Modifier
The modifier is used for case-sensitive and global matching:

Square brackets
Square brackets are used to find characters in a certain range:

Metacharacters
Metacharacter is a character with special meanings:

Quantifiers

RegExp object Method

Methods for String objects that support regular expressions

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.