How to use JavaScript and regular expressions for data verification

Source: Internet
Author: User
Taking advantage of the client's JavaScript, regular expressions in JavaScript can simplify data verification. Next I will share with you how to use JavaScript and regular expressions for data verification, if you are interested, refer to data verification as an important step for network application software to accept data from the client. After all, you need to ensure that the data meets the expected format before using the customer data. In network applications, you can select Tools on a specific platform, such as ASP.. NET, JSP, etc., or you can take advantage of the client's JavaScript, the regular expression in JavaScript can simplify data verification.

Regular Expression
Regular Expressions are a pattern matching tool that allows you to express the pattern in text mode. Therefore, regular expressions are a powerful tool for verifying text data. In addition to pattern matching, regular expressions can also be used for text replacement. The support for regular expressions has been extended since I first came into contact with regular expressions when I was using Perl on UNIX systems.
Note: If you have many other developers, regular expressions may be called RegEx or RegExp. Although regular expressions are powerful, their syntax is a bit "mysterious" and it takes some time to master them. Let's take a look at some basic knowledge about using regular expressions.

Basic syntax
The syntax of a regular expression can be used in a complex way. You may even need a whole book to explain this question. However, I will explain some of the basic knowledge to help you get a preliminary understanding of the regular expression.
A basic concept is anchor. It allows you to specify the start and end points of a string. The Escape Character (^) is used to specify the start point of a string, and the dollar sign ($) indicates the end point. If you need to include the escape character or dollar sign in the query string, you can use the escape sequence. The Escape Character () takes precedence over the escape character or dollar sign. In the following example, the word search is matched when it appears in a string.

^ Search $
In addition, you can also find a group of characters, as long as they are placed in square brackets, such as [and], matching characters must belong to this character group, one example is to search for matching numbers 1 to 5 in the range of [12345]. This regular expression can also be written in [1-5].
In many cases, you may need to specify the characters that can appear multiple times, or optional characters, question mark (?) This character is optional, and the plus sign (+) indicates that the character can appear once or multiple times. The asterisk (*) this character may not appear or appear multiple times.
Now let's take a look at how to apply these simple regular expressions to JavaScript.

JavaScript support
JavaScript has added support for regular expressions in version 1.2. browser support starts with Internet Explorer 4 and Netscape 4, all Firefox versions and most modern browsers contain JavaScript support. Regular Expressions can be used through JavaScript strings and RegExp.

Use string
Each JavaScript string can support regular expressions in three ways: match (), replace (), and search (), and test () of the object () the method also allows you to perform a test. The following information about the match (), replace (), and search () methods:
Match (): used for regular expression matching. If multiple matches appear, an array containing all matching results is returned. Each entry in the array is a copy containing matching data; if no matching value exists, a null value is returned.

Replace (): used for regular expression matching and replacing all matching values with new substrings. The first parameter of this method is a regular expression, and the second parameter is a replacement string.

Search (): used to search for matching values between a regular expression and a specified string. If a matching value exists, the index value of the string is returned. If no matching value exists,-1 is returned.

JavaScript also provides RegExp objects to create and use regular expressions.

RegExp
The RegExp object contains the regular expression pattern. The methods and attributes of this object can be used to match strings. There are two ways to create an instance of the RegExp object: the second parameter is optional when you use a constructor or a regular expression in text mode. This parameter specifies that the search is global (g) and case-insensitive (I) or, the Case sensitivity (gi) is ignored globally ). The following example uses the constructor to create a RegExp object. In this example, the case of the search object is ignored:

testRegExp = new RegExp("^search$","I")

You can use text to create the same instance (the part in the slash), as shown below:

testRegExp = /^search$/i

The RegExp object contains a large number of methods, but we only introduce one of the methods test. This method matches the specified string with a regular expression. If it succeeds, true is returned. If it fails, false is returned. This method can be applied to text strings or string variables. Basically, it allows you to perform regular expression matching on a string. The following example shows how to use this method:

TestRegExp =/search/I; if (testRegExp. test ("this is a search string") {document. write ("The string was found. ");} else {document. write ("No match found. ");} We can place it in a Web page to test: RegExp test  
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.