Replace substring(s) matching the given regular expression
(regexp, replacement)
parameter:
regexp: RegExp object or string
replacement: A string of the replacement text, or a function that generates the corresponding replacement text when called.
return:
Return a new string that has been replaced
describe:
replacement can be a string or a function. If it is a function, it will be called on each matching result, and the string it returns will be used as replacement text.
Pass the parameters of this function:
1) String matching this pattern
2) The string matching a parentheses subexpression in this pattern may be 0 or more such parameters
3) Integer, specify the position where the matching result appears in String
4) string itself
Example:
Copy the codeThe code is as follows:
// Make sure the word "javascript" is correct in the upper and lower case
(/javascript/i, 'JavaScript');
//Replace all double quotes with paired front and back single quotes
(/"([^"])"/g, "''$1''");
//Convert a separate name from the format "Mack, Xu" to "Xu Mack"
(/(\w+)\s*,\s*(\w+)/, "$2 $1");
//Captured the initial letter of all words in a string
(/\b\w+\b/g, function(word) {
return (0, 1).toUpperCase() + (1);
});