Home > Web Front-end > JS Tutorial > body text

Learn string.replace() function from jQuery.camelCase()_javascript skills

WBOY
Release: 2016-05-16 18:02:18
Original
1244 people have browsed it

Function
The function of the camelCase function is to convert the form background-color into camel case notation: backgroundColor.
This function is used in jQuery’s data function and many functions involving css.

Implementation of jQuery

Copy code The code is as follows:

//Regular Match
rdashAlpha = /-([a-z])/ig,
// Callback function when camelCase replaces string
fcamelCase = function( all, letter ) {
return letter.toUpperCase() ;
},
...
camelCase: function( string ) {
return string.replace( rdashAlpha, fcamelCase );
},

this The function itself is not difficult, it just calls the replace method of the String object. But in the spirit of learning, I still studied the replacement method.
Data reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

String.replace() syntax
str.replace(regexp|substr, newSubStr|function[, Non-standard flags]);

String.replace() parameter description
regexp: one for searching regular expressions
substr: one for searching for strings
newSubStr: a new string for replacement
function: a callback function, the return value of the function is used to replace the original matching string
flags: non-standard, similar to i, g, m of RegExp (Ignore case, search globally, match multiple lines)

Specify a string as the replacement object
You can use the following pattern in the string used for replacement:
$$ => Insert a $
$& => Insert matching substring
$` => Insert all characters before matching substring
$' => Insert all characters after matching substring
$n / $nn => This mode is only valid when the first parameter of the replace() method is RegExp and the regular expression contains parentheses.

Specify function as replacement object
Typical replacement function: function(str,p1,p2,offset,s){}
Parameter description:
str: matching string (similar $&)
p1,p2,...: This mode is only valid when the first parameter of the replace() method is RegExp and the regular expression contains parentheses. (Similar to $n / $nn)
offset: the offset of the matching substring
s: the string used for search

Get the camel case representation of the CSS property
Copy code The code is as follows:

String.prototype.camelCase=function(){
//all is the matching substring, and letter is p1, because [a-z] has parentheses
return this.replace(/-([a-z])/ig,function( all, letter,offset,s ) {
return letter.toUpperCase();
});
};
var cssText = 'h2n{n border-bottom:1px solid #eee;n background-color:#bbb;n }';
var newstr = cssText.camelCase();

Exchange the position of the matching string
Copy code The code is as follows:

var re = /(w )s(w )/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template