Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept and usage of regular expressions in JavaScript

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "the concept and usage of regular expressions in JavaScript". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Today I would like to share with you some knowledge about regular expressions and their application in javascript. Regular expressions are simple but not simple. For example, when my teacher used to teach us, he would have mastered this thing in 20 or 30 minutes. If he didn't get started, he would not be able to make it up for several days. So I studied and studied it very seriously at the beginning. Unexpectedly, regular expressions are not only concise in code, but also save a lot of trouble for front-end engineers in practice. As we all know, when users browse the page, the only thing they deal with with data is the form. There are actually many ways to validate the form. Next, I will share two ways for you, one is the ordinary and tedious method, the other is the regular expression, let's see what kind of effect it can bring to the form.

First of all, let's take a look at the regular version:

I feel that the method is too corny, this is a simple form effect made at the beginning of learning, without adding CSS stylesheets.

Take another look at the upgraded version of regular expressions:

Let's take a look at regular expressions.

A regular expression is an object that describes a character pattern, also known as normal representation, regular representation.

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

The characteristics of regular expressions are: 1. Very flexible, logical and functional; 2. You can quickly achieve complex control of strings in a very simple way. 3. For people who have just come into contact, it is more obscure.

Define regular expression 1: normal varreg=/ expression / additional parameter reg.test (v.value)

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

Pattern 1 of the expression: simple pattern var reg=/china/; 2: match the pattern var reg=/ ^\ wicked raceme; var reg=/ ^\ wicked @\ wt +. [a-zA-Z] {2jue 3} (. [a-zA-Z] {2jue 3})? $/

The method exec of the RegExp object retrieves the zoning of the regular expression in the character, returns the value found, and determines its location. Test retrieves the value specified in the string, and returns the true or false exec method: if no match is found, the return value is null;. If a match is found, it returns an array of results, /... / represents the beginning and end of a pattern ^ match string start $match string end\ s any blank character\ S any non-white space character\ d matches a numeric character, which is equivalent to [0-9]\ D any character except a number, equivalent to [^ 0-9]\ w matching a numeric, underscore, or alphabetic character Equivalent to [A-Za-z0-9]\ W any non-word character, equivalent to [^ a-zA-z0-9]. Any character except a newline character

{n} match the previous term n times {n,} match the previous term n times, or multiple times {nmagazine m} match the previous term at least n times, but not more than m times * match the previous term 0 or more times, which is equivalent to {0,} + 1 or more times of the previous term, equivalent to {1,}? Match the previous item 0 or 1 times, that is to say, the previous term is optional, which is equivalent to {0men1}

X | y matches x or y.

For example, "z | food" can match "z" or "food". "(z | f) ood" matches "zood" or "food". [xyz] character collection. Matches any of the characters contained. For example, "[abc]" can match "a" in "plain". [amurz] character range. Matches any character within the specified range. For example, "[a murz]" can match any lowercase character in the range of "a" to "z". Note: the range of characters can be represented only if the hyphen is within the character group and appears between two characters; if the beginning of the character group is out, it can only represent the hyphen itself. (pattern) matches the pattern and gets the match. The obtained match can be obtained from the resulting Matches collection, which uses the SubMatches collection in VBScript and $0 in JScript. $9 attribute. To match parenthesis characters, use "\ (" or "\)".

This is the end of "the concept and usage of regular expressions in JavaScript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report