This article mainly introduces how to use replace in javascript. Using replace and regular expressions to implement the string trim method is of reference value, interested friends can refer to this article for details about how to use replace in javascript. Using replace and regular expressions to implement the string trim method together has some reference value, for more information, see
ECMAScript provides the replace () method. This method receives two parameters. The first parameter can be a RegExp object or a string, and the second parameter can be a string or a function. Now let's explain in detail several possible situations.
1. When both parameters are strings
Var text = 'cat, bat, sat, fat'; // find at in the string, replace at with ond, and replace var result = text only once. replace ('at', 'ond '); // "cond, bat, sat, fat" console. log (result );
2. The first parameter is the RegExp object, and the second parameter is a string.
We can find that only the first at is replaced in this case. If you want to replace all at, you must use the RegExp object.
Var text = 'cat, bat, sat, fat'; // use/at/g to match at globally and replace var result = text with ond. replace (/at/g, 'ond '); // cond, bond, sond, fond console. log (result );
3. Consider capturing groups in RegExp objects.
RegExp has nine attributes used to store capture groups. $1, $2... $9 are used to store the first to nine matching capture groups. You can access these attributes to obtain the stored values.
Var text = 'cat, bat, sat, fat'; // use /(. at)/g parentheses are the capture group, and there is only one, so the matched value is stored in $1 var result = text. replace (/(. at)/g, '$ ($1)'); // $ (cat), $ (bat), $ (sat), $ (fat) console. log (result );
4. When the second parameter is a function, the RegExp object does not contain a capture group.
Var text = 'cat, bat, sat, fat'; // use/at/g to match all at in the string and replace it with ond, // The parameters of the function are: currently matched characters, the position of the currently matched characters, and the original string var result = text. replace (/at/g, function (match, pos, originalText) {console. log (match + ''+ pos); return 'ond}); console. log (result); // output/* at 1 dd.html: 12: 9 at 6 dd.html: 12: 9 at 11 dd.html: 12: 9 at 16 dd.html: 12: 9 cond, bond, sond, fond dd.html: 16: 5 */
5. If the second parameter is a function, a capture group exists in the RegExp object.
Var text = 'cat, bat, sat, fat'; // use /(. at)/g matches all at in the string and replaces it with ond. // when the regular expression contains a capture group, the function parameter is a pattern match item at a time, match of the first capture group, // match of the second capture group... the position of the match in the string. The original string var result = text. replace (/. (at)/g, function () {console. log (arguments [0] + ''+ arguments [1] +'' + arguments [2]); return 'ond}); console. log (result); // output/* cat at 1 bat at 6 sat at 11 fat at 16 cond, bond, sond, fond */
The above are all applicable situations of the replace method. Below we use replace and regular expression to implement the string trim method together.
(Function (myFrame) {myFrame. trim = function (str) {// 'Hello world' return str. replace (/(^ \ s *) | (\ s * $)/g, '') ;}; window. myFrame = myFrame;}) (window. myFrame | {}); // test var str = 'Hello world' console. log (str. length); // 15 console. log (myFrame. trim (str ). length); // 11
The above is a detailed summary of the methods used by replace in javascript. For more information, see other related articles in the first PHP community!