Regular Expressions in JS

Source: Internet
Author: User

Introduction: in computer science, a single string is used to describe or match a series of strings that conform to a certain syntax rule. In many text editors or other tools, regular expressions are usually used to retrieve or replace text content that meets a certain pattern. Regular Expressions verify that a series of regular expressions are related to regular expressions. The match () and replace () Methods return the replace of strings after text replacement based on regular expressions. Syntax: The stringObj. replace (rgExp, replaceText) match () method searches strings in regular expression mode and returns the results containing the search result as an array. Syntax: The stringObj. match (rgExp) Regular Expression is written between two dashes. The last double slash is followed by g. the string to be searched. g Represents the global meaning. Of course, if the content to be searched is case-insensitive, add an I letter to the end of g. Purpose: 1. Test the mode in the string. For example, you can test the input string to check whether the phone number mode or credit card number mode is displayed in the string. Or whether the user name is entered as a space. This is called data verification. 2. Replace the text. You can use regular expressions to identify a specific text in a document, delete the text completely, or replace it with other text. 3. Extract the substring from the string based on the pattern matching. You can search for specific texts in a document or input domain.
Example 1: <scriptlanguage = "javascript" type = "text/javascript"> varstr = "windows is an excellent system. I like windows! "Varreg =/Windows/gi; vartemp = str. replace (reg, "Linux"); alert (temp); </script> Example 2: Verify the QQ number, the value above five digits indicates a number starting with xxx. $ represents a number ending with xxx. \ d represents a number 0-9. The parentheses (5,) represent a method of a regular expression object with more than five digits: test, true is displayed if it meets the requirements. Otherwise, false <scriptlanguage = "javascript" type = "text/javascript"> varqq = 5569 varreg =/^ \ d (5 ,) $/g; if (! Reg. test (qq) {alert ("Incorrect qq number format! "); Return ;}</script>

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.