1. Basic Syntax: replace (regexp | substr, newSubStr | function [, flags]); 2. parameter description: 1) parameter 1: a) regexp: the content of a RegExp object that matches this regular expression will be returned by the second parameter & amp; 20540; replaced by B) substr:
I. Basic Syntax:
Replace (regexp | substr, newSubStr | function [, flags]);
Ii. Parameter introduction:
1) parameter 1:
A) regexp: a RegExp object. The content matched by this regular expression will be replaced by the return value of the second parameter.
B) substr: a String to be replaced.
2) parameter 2:
A) newSubStr: Replace the matching part of the first parameter in the original string. this string can be inserted with some special variable names. for example, $, $ ', $ &, $1... 99 etc.
The special variable names are described as follows:
$: String "$ ".
$ &: Indicates the substring that the first parameter matches
$ ': Content on the left side of the matched substring $.
$ ': The content on the right of the matched substring $.
$ N or $ nn: If n or nn is a decimal number and the first parameter of the replace method is a regular expression, then $ n indicates the nth substring in the regular expression that matches the string.
B) function (str, $1 [, $2,..., $99], offset, s): Create a New substring. The parameters are described as follows:
Str: matched substring
$1... 99: the nth substring matching the string. The first parameter provided for replacement is a regular expression object. (Corresponding to $1, $2, etc .)
Offset: match the starting position of the substring in the string (starting from 0), for example, "abcd", regular/bc/, offset = 1.
S: string of the current operation
3) parameter 3 (optional ):
Flags: Specifies the matching mode of the regular expression. Optional values:
G: Global replacement
I: case insensitive
M: multiline replacement
Y :?
Iii. Practice
1) string Inversion
var name = "Hello Benjamin";console.log(name.replace(/(\w+)(\s+)(\w+)/g,"$3$2$1"));//Benjamin Hello
2) General conversion to camper
Var name = "border-top-width"; // The console is matched only once without g. log (name. replace (/-(\ w)/g, function (str, $1) {return $1. toUpperCase () ;})); // borderTopWidth
3) The Hump type is converted to the general type.
Var name = "borderTopWidth"; console. log (name. replace (/[A-Z]/g, "$ &". toLowerCase (); // borderTopWidth, cannot be converted correctly! It is because "$ &". toLowerCase () is first converted to lower case, or "$ &", so there are changes. // Check the following example to find the console. log (name. replace (/[A-Z]/g, "XX $ & YY ". toLowerCase (); // borderxxTyyopxxWyyidth, First Run "XX $ & YY ". toLowerCase () is converted to lowercase, and then replaced with a regular expression. Console. log (name. replace (/[A-Z]/g, function (match) {return "-" + match. toLowerCase ();}));
4) convert Fahrenheit to Celsius
Function F2C (str) {// \ B matches the return String (str). replace (/(\ d + (\. \ d *)?) boundary of a word *)?) F \ B/g, function (str, $1, $2, offset, s) {return ($1-32)/1.8 + "C ";});} console. log (F2C ("21.2F"); //-6 °C
5) Replacement of common strings
var name = "Hello Benjamin";console.log(name.replace("Hello","Welcome to"));//Welcome to Benjamin
6) use flags
Var name = "Welcome to Benjamin website, my url is http://blog.csdn.net/cuew1987 of Benjamin -- front-end development engineer" console. log (name. replace ("Benjamin", "Ben", "gi"); // global replacement, case-insensitive, Welcome to Ben website, my url is http://blog.csdn.net/cuew1987 of Ben-front-end development engineer
Reprinted, please respect originality, and indicate the source Benjamin-front-end attacker. The address on this page is http://blog.csdn.net/cuew1987/article/details/17528653. Thank you!