Replace method of JS

  • 2020-03-30 00:46:19
  • OfStack

Definition and usage
The replace() method is used to replace some characters in a string with others, or to replace a substring that matches the regular expression.

grammar
StringObject. Replace (regexp/substr, replacement)

parameter describe The regexp/substr

A necessity. RegExp object that specifies the substring or pattern to be replaced.

Note that if the value is a string, use it as the direct quant text mode to retrieve, rather than being converted to a RegExp object first.

replacement A necessity. A string value. Specifies the function that replaces text or generates replacement text.

The return value

A new string is obtained after replacing the first or all matches of the regexp with a replacement.

instructions

The replace() method of the string stringObject performs a find and replace operation. It looks for substrings in stringObject that match the regexp, and then replaces them with replacements. If the regexp has the global flag g, the replace() method replaces all matched substrings. Otherwise, it simply replaces the first matched substring.

Replacement can be a string or a function. If it is a string, each match will be replaced by a string. But the $character in replacement has a specific meaning. As shown in the following table, it indicates that the string from the pattern match will be used for substitution.

character Replace text $1, $2... , $99 Text that matches the 1st through 99th subexpressions in regexp. $& A substring that matches the regexp. $` The text to the left of the matching substring. $' The text to the right of the matching substring. $$ Direct quantity sign.

Note: ECMAScript v3 states that the replacement () method's argument replacement can be a function rather than a string. In this case, the function is called for each match, and the string it returns is used as replacement text. The first argument to this function is the string that matches the pattern. The next argument is a string that matches the subexpression in the pattern, and you can have 0 or more such arguments. The next parameter is an integer that declares the location where the match occurs in the stringObject. The last parameter is stringObject itself.

More base instances can be viewed here:

The replacement () method can be a function instead of a string. In this case, the function is called for each match, and the string it returns is used as replacement text. The first argument to this function is the string that matches the pattern. The next argument is a string that matches the subexpression in the pattern, and you can have 0 or more such arguments. The next parameter is an integer that declares the location where the match occurs in the stringObject. The last parameter is stringObject itself.

The following sections show several ways of repalce with javascript regular representations, some of which we rarely see elsewhere, such as the second and third party methods.


//The following example takes two arguments to the url and returns the real url before urlRewrite
var reg=new RegExp("(http://www.qidian.com/BookReader/)(\d+),(\d+).aspx","gmi");
var url="http://www.qidian.com/BookReader/1017141,20361055.aspx";
//Method one, the simplest and most common way
var rep=url.replace(reg,"$1ShowBook.aspx?bookId=$2&chapterId=$3");
alert(rep);
//The second method USES the callback function with fixed parameters
var rep2=url.replace(reg,function(m,p1,p2,p3){return p1+"ShowBook.aspx?bookId="+p3+"&chapterId="+p3});
alert(rep2);
//The third way, the use of non-fixed parameters of the callback function
var rep3=url.replace(reg,function(){var args=arguments; return args[1]+"ShowBook.aspx?bookId="+args[2]+"&chapterId="+args[3];});
alert(rep3);

//Methods four
//Method 4 is similar to method 3 in that you can get parameters separately, in addition to returning the replaced string
[code]
var bookId;
var chapterId;
function capText()
{
    var args=arguments; 
    bookId=args[2];
    chapterId=args[3];
    return args[1]+"ShowBook.aspx?bookId="+args[2]+"&chapterId="+args[3];
}
var rep4=url.replace(reg,capText);
alert(rep4);
alert(bookId);
alert(chapterId);

//In addition to using the replace method to get the grouping of the regular representation, test and exec methods can also be used to get the grouping, but in a different way
var reg2=new RegExp("(http://www.qidian.com/BookReader/)(\d+),(\d+).aspx","gmi");
var m=reg2.exec("http://www.qidian.com/BookReader/1017141,20361055.aspx");
var s="";
//Gets all the groups
for (i = 0; i < m.length; i++) {
      s = s + m[i] + "n";      
      }
alert(s);
bookId=m[2];
chapterId=m[3];
alert(bookId);
alert(chapterId);

//Use the test method to get the grouping
var reg3=new RegExp("(http://www.qidian.com/BookReader/)(\d+),(\d+).aspx","gmi");
reg3.test("http://www.qidian.com/BookReader/1017141,20361055.aspx");
//Get three groups
alert(RegExp.$1); 
alert(RegExp.$2);
alert(RegExp.$3);
    var str="www.baidu.com";
        //STR. The format (" good ", "q")              
        str.replace(new RegExp("(\.)(bai)du","g"),function(){            
            for(var i=0;i<arguments.length;i++)
            {
                    document.write(arguments[i]+"<br/>");
            }
            document.write("-------------------------------------------------<br/>");
        }); 

  Two examples (prove that the result of replace passing in the regular parameter and character passing parameter are different) :


Related articles: