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>