Discussion on replace of method in jQuery

  • 2020-06-12 08:25:57
  • OfStack

jquery today reading the source code, I found a oneself never had problems before, is replece () function for the second parameter, just know replace before () the second parameter for function, but don't know how to operate, used to function as replace in today to see the source code (2) parameters, feel more difficult to read, so ready to sort out this function...

grammar

stringObject.replace( regexp/substr, replacement)

The return value

Returns a new string that replaces the first or desired match of regexp with replacement

When the argument to the replace() method replacement is a function, in which case every match is called, the string returned by the function is used as replacement text. The first argument to this function is the string that matches the pattern. The next argument is the string that matches the subexpression in the pattern, and can have zero or more such arguments. The next argument is an integer that declares where the match occurs in stringObject. The last parameter is stringObject itself. This sentence is copied to w3cschool. For me, I can't quite understand the above sentence, nor can I simply describe it in my own words, so I can only use examples to illustrate this one


var string = "abc123-ii";
string.replace(/(\d)-([\da-z])/g,function( str1, str2, str3,str4,str5){
         console.log( str1 );// 3-i
         console.log( str2 );// 3( The first 1 A capture )
         console.log( str3 );// i( The first 2 Three do not capture groups )
         console.log( str4 );// 5( Match in string The position that appears in )
         console.log( str5 );// abc123-ii ( string Itself)
         return "I";
})

Above is today I am looking at the jquery source code


camelCase: function( string ) {
        return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
    },
fcamelCase = function( all, letter ) {
        return letter.toUpperCase();
    };

I don't know what this function looks like now That I understand it

And now I remember when I was using repleace() in the past, I encountered strange symbols in the form of "$1,$2" and so on. Now come at night to do a solution to this problem

At $1, $2, $3... Means capture 1,2,3...


var string = "abc123-ii";
console.log(string.replace(/(\d)-([\da-z])/g, "$1")); // With the capture group 1(3) To replace /(\d)-([\da-z])/g

$ & Represents a substring that matches regexp


var string = "abc123-ii";
console.log(string.replace(/(\d)-([\da-z])/g, "$&")); // With the regexp The matching string (3-i) To replace /(\d)-([\da-z])/g

$' represents the text to the left of the matching substring


var string = "abc123-ii";
console.log(string.replace(/(\d)-([\da-z])/g, "$`")); // With the text to the left of the matching string ( abc12 ) to replace /(\d)-([\da-z])/g

$' represents the text to the right of the matching substring


var string = "abc123-ii";
console.log(string.replace(/(\d)-([\da-z])/g, "$ ' ")); // Replace with the text to the right of the matching string /(\d)-([\da-z])/g

$$is directly the $symbol


var string = "abc123-ii";
console.log(string.replace(/(\d)-([\da-z])/g, "$$")); // with $ Symbol to replace /(\d)-([\da-z])/g

The above is where I am not clear about the use of replace () method. If there is anything wrong with the writing, or there are some examples about the better use of this method, I hope you can share them...

This is the end of this article, I hope you enjoy it.


Related articles: