JavaScript Learning 1: regular expressions,

Source: Internet
Author: User

JavaScript Learning 1: regular expressions,

In fact, I knew the concept of Regular Expressions before I learned JavaScript. I remember the first time I heard that I was studying a professional course, this course was called the theory of formal language and automation. now, I still clearly remember the pain during the course. After one semester, I did not know what the course was about or what it was used?

Later, when preparing for the soft exam, I got to know the content again. I finally learned that this course is about a thing called grammar, the management system divides grammar into four categories: Zero grammar (phrase grammar), 1 grammar (context-related grammar), and 2 Grammar (context-independent grammar) and type 3 grammar (regular syntax). What we want to talk about today is the regular expression generated by regular syntax, that is, regular expression.

What is a regular expression?

RE (RegularExpression) is an object that describes the character mode. The RegExp class in JavaScript represents a regular expression, while both String and RegExp define functions that use regular expressions for powerful pattern matching and text retrieval and replacement.

Why use a regular expression?

Assume that you need to enter the name, address, date of birth, and other information in the HTML form, before submitting the form to the server for further processing, the JavaScript program checks the form to confirm that the user input is valid. The regular expression in the program is mainly used to verify whether the data entered by the client meets the requirements. This can save a lot of system resources on the server, improve system performance, and provide a better user experience.

How to use a regular expression?

There are two ways to create a regular expression in JavaScript. One is to use the new operator, and the other is to use the literal expression.

Create with the new operator

Varstr = new RegExp ('pattern string', 'pattern modifier (optional )');

Create with literal

Varstr =/mode string/mode modifier;

This pattern modifier is an optional parameter, mainly including the following

 

After the regular expression is created, the RegExp object contains two methods: text () and exec (). The functions are similar and used to test string matching. The Test () method finds whether a specified regular expression exists in the string and returns a Boolean value. The Exec () method is also used to find the specified regular expression. If execution is successful, an array of related information containing the query string is returned. Otherwise, null is returned.

After learning the basic knowledge above, we will go to the next step. The regular expression metacharacters are characters with special meanings. They have some special functions to control the matching mode, the metacharacter after the backslash will lose its special meaning, and we are getting closer and closer to it in actual use. The following describes the most common character classes:


Of course, there are other character classes, such as blank characters, anchor characters, replacement characters, and record characters. I will not introduce them more here. Let's take a look at how to use the character classes to write regular expressions by themselves, complete the verification we want.

First, create a verification expression for the mobile phone number.

<span style="font-size:18px;"><span style="font-size:18px;"></span>
<Span style = "font-size: 18px;"> var pattern =/[1] [0-9] {10}/; var tel = '000000'; if (! Pattern. test (tel) alert ('enter the correct phone number! '); </Span>

Finally, we use the knowledge we have just learned to make a function to verify the email address.

<span style="font-size:18px;"><span style="font-size:18px;"></span>
<Span style = "font-size: 18px;"> function VerifyMail (mail) {var pattern =/^ ([\ w \. \-] +) @ ([\ w \-] + )\. ([a-zA-Z] {2, 4}) $/; var str = mail; if (pattern. test (str) alert ('the email address you entered is correct! '); Elsealert (' sorry! Enter a valid email address! ') ;}; // Call the function to verify whether the email address is correct. VerifyMail ('jaidfjai @ qq.com'); </span>

Summary: JavaScript learning is very important. The reason for summarizing the content of a regular expression is that it was previously considered magical and not very understandable. Furthermore, it is very important, during Web development, it is common to express the submission, so the verification of the submitted content will follow. We cannot submit all the verification operations to the server, this is very inefficient.

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.