Html5 knowledge point: Regular Expressions and html5 Regular Expressions

Source: Internet
Author: User
Tags valid email address

Html5 knowledge point: Regular Expressions and html5 Regular Expressions

Many people think that regular expressions are used for form verification, which is not accurate. Regular Expressions are widely used in many software applications, including Linux, Unix, VBScript, Java, PHP, and other development environments, and many applications can apply regular expressions.

1. RegEx history.

First, let's clear a misunderstanding. Some people always think that regular expressions are self-invented by JS, which is of course incorrect. In 1956, a mathematician named Stephen Kleene published a paper titled neural network representation on the basis of McCulloch and pitts's early work, the first time we introduced the concept of regular expressions. The so-called regular expression is used to describe the expression called "the algebra of the regular set". Therefore, the term "Regular Expression" is used.

 

 

Later, it was discovered that this work could be applied to some early studies using ken Thompson's computation search algorithm. Ken Thompson was the principal inventor of Unix, the first one that uses regular expressions in the application is the qed editor in Unix. Since then, regular expressions have been an important part of text-based editors and search tools.

The development history of regular expressions is not particularly long-term, but it was quickly adopted by major programming languages after its launch. This is mainly due to its own characteristics:

First, compared with the traditional verification method, regular expressions can perform the verification work more efficiently.

Second, the ability to capture strings, regular expressions can also well complete the work, such as intercepting the url domain name or other content.

Third, flexible expression and concise writing. Regular Expressions can be used to easily implement various complex forms of verification and various string processing.

Ii. Definition of Regular Expressions

Regular expressions describe a string matching mode, which can be used to retrieve whether a string contains a certain seed string, replace the matched substring or extract the substring that meets a certain condition from the substring.

A regular expression is a text format consisting of common characters (a-z) and special characters (also called metacharacters. A regular expression is used as a template to match a character pattern with the searched string.

2.1 common characters

It consists of all the print and non-print characters that are not displayed and specified as metacharacters. This includes all uppercase and lowercase characters, all numbers, all punctuation marks, and some symbols.

Special Character 2.2

Special characters refer to characters with special meanings, such as "*. *. simply put, it means any string. If you want to find a file with * in the file name, you need to escape *, that is, add \. ls \. exe.

Special characters $, (), *, + ,...[,?, \, ^, |

$ Matches the end position of the input string.

() Mark the start and end positions of a subexpression.

* Matches the previous subexpression zero or multiple times.

+ Match the previous subexpression once or multiple times.

. Match any single character except linefeed \ n.

[Mark the beginning of a brackets.

? Match the previous subexpression zero or once.

{Mark the start of a qualified expression.

\ Mark the next character as a special character, an original character, or backward reference, or an octal escape character.

^ Matches the start position of the input string. Unless used in the square brackets expression, this character set is not accepted.

| Specify an option between the two items.

2.3 qualifier

A qualifier is used to specify how many times a given component of a regular expression must appear to match. There are *, + ,?, There are 6 types of {n}, {n ,}, {n, m.

* Matches the previous subexpression zero or multiple times.

+ Match the preceding self-expression once or multiple times ,? Match the previous subexpression zero or once.

{N} n is a non-negative integer that matches the specified n times.

{N ,}n is a non-negative integer and matches at least n times.

Both {n, m} m and n are non-negative integers, and n <= m. They must be matched at least n times and at most m times.

2.4 positioning Operator

Used to describe the boundary of a string or word. ^ and $ respectively indicate the start and end of a string, \ B describes the boundary before or after a single meal, and \ B indicates a non-word boundary, the delimiters cannot be used.

Iii. Application of regular expressions in the web

Regular Expressions are widely used in web systems. They can detect data formats, replace related texts, and extract content of interest.

For example, verify the validity of the E-mail address in the string.

The email address format is <user name @ Domain Name>. For user names, except for numbers and letters, some allow "-" and some allow ". ", or both, or allow other special characters. We can only determine this based on the actual situation. It is assumed that in addition to letters and numbers, ".", "-", and "." and "-" are not given to you at the beginning and end. "," "." "-" Cannot be connected. In the domain name, only numbers and letters are allowed to appear "-" And cannot appear in the first and last places. "connection, we can know from the domain name that the last segment is more than one character and only a letter. Based on the above content, we can write an expression to determine whether the string is a valid email address.

Is it troublesome to listen to rules alone? Don't worry. The Division explanation is as follows:

^: Match starts

([A-z0-9A-Z] + [-| \.]?) +: The combination of numbers or letters greater than one, "-" or "." Must be repeated more than once.

[A-z0-9A-Z]: the user name ends with a number or letter.

@: Match '@' [a-z0-9A-Z] + match multiple-digit characters or letters

(-[A-z0-9A-Z] + )? : Match one plus multiple digits or Letters 0 times or 1 time.

\.: Match "."

+: Match the content in the brackets multiple times

[A-zA-Z] {2,} match more than 2 letters

$: The matching end combines the above items to match a comprehensive email address. The regular expression is as follows:

^ ([A-z0-9A-Z] + [-| \.]?) + [A-z0-9A-Z] @ ([a-z0-9A-Z] + (-[a-z0-9A-Z] + )? \.) + [A-zA-Z] {2,} $

Complicated? The whole process is quite complicated, but when you separate functions and implement them one by one and then combine them together, it is actually not that complicated.

Full-stack engineers need to understand Regular Expressions for various form verification and string processing. Architects also need to understand Regular Expressions because regular expressions are used in the framework. As mentioned above, regular expressions are naturally one of the languages we must learn. What are the characteristics of HTML5 regular expression project development courses? The biggest feature is the outstanding practicality and efficiency. Learning regular expressions based on the knowledge of the front-end enables you to better think about and learn regular expressions at a higher level.

How to teach the regular expression project development course? The following points are highlighted:

First, step by step. This course begins with the basics, so that students can familiarize themselves with more string operations and master the string verification methods. Then, find a real online project based on the actual situation, analyze the verification methods and methods, and explain the usage of regular expressions.

Second, project-driven. Project-driven knowledge point learning throughout the process. After understanding the actual needs, we use regular expressions to meet our needs.

Third, focus on practice. After the students finish the instructor's face-to-face teaching, they must complete another new project.

The regular expression project development course contains the regular expression basics. Advanced usage and common verification methods are described as follows:

Part 1: Basics of regular expressions. This section mainly includes three aspects:

Regular Expression basics. The content mainly includes regular syntax, common symbols, and simple form verification.

Part 2: advanced usage and complex Regular Expression verification.

Part 3: Find a specific project and see the benefits of regular expressions in practice.

Iv. Conclusion

Regular expressions have simple syntax and powerful functions, especially for data verification. In daily data processing and software development, regular expressions have become an indispensable tool, I believe that with the development of web networks, regular expression applications will become more and more powerful and easy to use.

Source: qianfeng html5

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.