Thoroughly understand the replace function in javascript

The language of javascript has always been like a beautiful woman wearing a veil, always unable to see clearly, unable to understand, has always focused on the server side, and has never paid special attention to it. Until recent years, javascript has become more and more important and more and more important. The more versatile. Recently, I have been relatively close to the front end. Take this opportunity to consolidate the relevant knowledge points. Link: Thoroughly understand the replace function in javascript_MOOC Notes

1. Getting to know replace

There are two replace functions in js, one is location.replace(url) to jump to a new url

A string.replace("xx","yy") replaces the string and returns a new string, this method does not change the string itself

location.replace(url) seamless jump (navigate the current link to a new url without saving history)

In contrast, location.href="url" has a trace jump (navigate the current link to a new url and save the history) This is not clear

Many beginners of the string.replace function will think that this is the same as Replace in C#, but it is not the same. Replace in js is more flexible.

The most basic usage is to simply replace strings. Let's look at an example:

var str = "abcd-abcd-abcd";var result = str.replace("a", "");console.log(result);//Output bcd-abcd-abcd//When the first parameter is a When a simple string, only the first occurrence is replaced

2. Enter the regular expression of replace

Parameter 1: regular expression object or literal (support gi mode) g global match i ignore case

Parameter two: the string to be replaced or a function

Related references that can use regular expressions

If it is a function, replace it with the return value of the function

Parameters for this function:

match The substring to match. (Corresponds to $& above.)

p1,p2, ... If the first parameter of the replace() method is a RegExp object, it represents the string matched by the nth parenthesis. (Corresponding to $1, $2, etc. above.)

offset The offset of the matched substring in the original string. (For example, if the original string is "abcd" and the matched substring is "bc", then this parameter will be 1)

string The original string to be matched.

References to regular expressions

$$ Inserts a "$".

$& inserts the matched substring.

$` Inserts to the left of the currently matched substring.

$' Insert to the right of the currently matched substring.

$n If the first argument is a RegExp object, and n is a non-negative integer less than 100, then insert the nth parenthesis matched string.

3. Come to a round of demos with comments to thoroughly understand the replace function in javascript:

Global match:

var str = "abcd-abcd-abcd";var result = str.replace(/a/g, "e");console.log(result);//output ebcd-ebcd-ebcd//g matches all a globally strings will be replaced

Case matching is ignored:

var str = "abcd-abcd-abcd";var result = str.replace(/A/i, "e");console.log(result);//output ebcd-abcd-abcd//ignore case matching But no global matching is done so only the first a is replaced

Ignore case and match globally:

var str = "abcd-abcd-abcd";var result = str.replace(/A/gi, "e");console.log(result);// output ebcd-ebcd-ebcd// ignore case and global matches all a's are replaced

Use function as the second parameter:

var str = "abcd-abcd-1234"; var result = str.replace(/([az]*)-([az]*)/gi, function(match,p1,p2,offset,str){ console. log(match); //abcd-abcd matched content console.log(p1); //matched content in the first bracket console.log(p2); //matched content in the second bracket console.log( offset);//0 index (offset) of the matched string console.log(str); //original string return [p1,p2].join("+");});console.log (result);// output abcd+abcd+1234// abcd-abcd in the original string is matched and replaced with the content returned by function

Quote the regex match in the argument:

var str = "a b";var result = str.replace(/(\w+)\s(\w+)/gi, "$2 $1"); console.log(result); //output b a // $1 here $2 refers to the content matched by the first and second parentheses in the regular expression respectively

Quote the content to the left of the match in the argument:

var str = 'abc'; var result = str.replace(/b/g, "$`"); //$` represents the content on the left side of the matched character console.log(result);//Output: aac

Quote the right side of the match in the argument:

var str = 'abc'; var result = str.replace(/b/g, "$'"); //$' represents the content on the right side of the matched character console.log(result);//Output: acc

Use a regular expression object:

 var str = "a b"; var reg = new RegExp(/(\w+)\s(\w+)/, "gi"); //You can also use regular representation objects as parameters var result = str.replace(reg , "$2 $1"); console.log(result); //output b a

With these examples and the noun explanation at the beginning, I believe you have thoroughly understood the replace function in javascript!

Guess you like

Origin blog.csdn.net/gcyaozuodashen/article/details/127869291