Concepts and Applications of Regular Expressions in JavaScript,

Source: Internet
Author: User

Concepts and Applications of Regular Expressions in JavaScript,

Today I will share with you some knowledge about regular expressions and Their Applications in javascript. Regular Expressions are simple but not simple. For example, when my teacher gave us a lecture about this thing, I became proficient in 20 or 30 minutes, once you have no entry, you cannot make up for it in a few days. So I learned and studied it very seriously at the beginning. I did not expect that the regular expression is not only concise, but also saves front-end engineers a lot of trouble in actual operations. In general, when a user browses a page, the only way to deal with data is the form. There are actually many ways to verify the form. Next, I will share with you two ways: one is a common and tedious method, and the other is a regular expression to see what effect it can bring to the form.

First, let's take a look at the common version:

I personally feel that the method is too earthy. This is a simple form effect when I first started learning, without adding a CSS style sheet.

Let's take a look at the upgraded regular expression:

 

Let's take a look at regular expressions.

A regular expression is an object that describes the character pattern, also known as regular notation and regular notation.

Regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern.

The features of regular expressions are: 1. Flexibility, logic, and functionality are very strong; 2. It can quickly achieve complex String Control in an extremely simple way. 3. It is difficult for new contacts.

Define Regular Expression 1: Normal Mode varreg =/expression/additional parameter reg.test(v.value)

2: constructor ModeVar reg = new RegExp ("expression", "additional parameter) var reg = new RegExp (" china ");

Expression mode 1: simple mode var reg =/china/; 2: compliant mode var reg =/^ \ w + $ /; var reg =/^ \ w + @ \ w +. [a-zA-Z] {2, 3 }(. [a-zA-Z] {2, 3 })? $ /;

RegExp object method exec searches for the regular expression, returns the found value, and determines its location test searches for the specified value in the string, returns the true or false exec method: if no match is found, the return value is null. If a match is found, a result array is returned ,/... /Represents the start of a pattern and the start of the end ^ match string $ match the end of the string \ s any blank character \ S any non-blank character \ d match a number character, it is equivalent to [0-9] \ D. It is equivalent to [^ 0-9] \ w matching a number, underline, or letter, equivalent to [A-Za-z0-9 _] \ W any non-single character, equivalent to [^ a-zA-z0-9 _]. any character except line breaks

{N} matches the previous item n times {n,} matches the previous item n times, or matches the previous item multiple times {n, m} At Least n times, but cannot exceed m times * match the first item 0 or multiple times, equivalent to {0,} + match the first item 1 or multiple times, equivalent to {1 ,}? Match the first item 0 or 1, that is, the first item is optional, equivalent to {0, 1}

X | y matches x or y.

For example, "z | food" can match "z" or "food ". "(Z | f) ood" matches "zood" or "food ". [Xyz] Character Set combination. Match any character in it. For example, "[abc]" can match "a" in "plain ". [A-z] character range. Matches any character in the specified range. For example, "[a-z]" can match any lowercase letter in the range of "a" to "z. Note: only when a hyphen is in a character group and appears between two characters can the range of the characters be expressed. If a group starts with a hyphen, it can only represent the character itself. (pattern) matches pattern and obtains this match. The obtained match can be obtained from the generated Matches set. The SubMatches set is used in VBScript, and $0… is used in JScript... $9 attribute. To match the parentheses, use "\ (" or "\)".

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.