1. Match method
The match () method retrieves the specified value within a string, or finds a match for one or more regular expressions.
The return value of the match () method is: An array that holds the matching results .
2. Replace method
The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.
The return value of the Replace method is: a new string .
3. Description
The parameters of the above 2 methods are mainly to add global g when using regular expressions, so that the string can be fully matched or replaced.
Example code:
<!DOCTYPE HTML><HTMLLang= "zh"> <Head> <MetaCharSet= "UTF-8" /> <title>Match and replace methods for strings in JavaScript</title> </Head> <Body> <!--Note that the SRC path should be - <Scriptsrc= "Js/jquery-1.12.4.min.js"type= "Text/javascript"CharSet= "Utf-8"></Script> <Scripttype= "Text/javascript"> varStr= "1 plus 2 equal 3"; //The match method returns an array of values vararr=Str.match (/[0-9]/g) Console.log (arr); varNew_str=Str.replace (/[0-9]/G,'Newstr'); //Replace method returns a value of new stringConsole.log (NEW_STR)</Script> </Body></HTML>
The console output is:
Match and replace methods for strings in JavaScript