Function
The function of camelCase function is to convert the form background-color into camel notation: backgroundColor.
This function is useful in jQuery's data function and many functions involving css.
jQuery implementation
//Regular match
rdashAlpha = /-([a-z])/ig,
// Callback function when camelCase replaces string
fcamelCase = function( all, letter ) {
return ();
},
...
camelCase: function( string ) {
return ( rdashAlpha, fcamelCase );
},
This function itself is not difficult, it is called the replace method of the String object. However, based on the attitude of learning, I still studied the replacement method.
Information reference: /en/JavaScript/Reference/Global_Objects/String/replace
()grammar
(regexp|substr, newSubStr|function[, Non-standard flags]);
() Parameter description
regexp: A search regular expression
substr: a string for searching
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 (ignoring case, whether to search globally, matching multiple lines)
Specify a string as a replacement object
In the string used for replacement you can use the following pattern:
$$ => Insert a $
$& => Insert the matching substring
$` => Insert all characters before matching substring
$' => Insert all characters after matching substring
$n / $nn => This pattern is valid only if the first parameter of the replace() method is RegExp and the regular expression contains brackets.
Specify a function as a replacement object
Typical replacement function: function(str,p1,p2,offset,s){}
Parameter description:
str: matching string (similar to $&)
p1,p2,...: This pattern is valid only if the first parameter of the replace() method is RegExp and the regular expression contains brackets. (similar to $n/$nn)
offset: Match the offset of the substring
s: string used for search
Get the camel representation of CSS attribute
=function(){
//all is the matching substring, while letter is p1, because [a-z] adds brackets
return (/-([a-z])/ig,function( all, letter,offset,s ) {
return ();
});
};
var cssText = 'h2\n{\n border-bottom:1px solid #eee;\n background-color:#bbb;\n}';
var newstr = ();
Swap the position of matching strings
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = (re, "$2, $1");
The function of camelCase function is to convert the form background-color into camel notation: backgroundColor.
This function is useful in jQuery's data function and many functions involving css.
jQuery implementation
Copy the codeThe code is as follows:
//Regular match
rdashAlpha = /-([a-z])/ig,
// Callback function when camelCase replaces string
fcamelCase = function( all, letter ) {
return ();
},
...
camelCase: function( string ) {
return ( rdashAlpha, fcamelCase );
},
This function itself is not difficult, it is called the replace method of the String object. However, based on the attitude of learning, I still studied the replacement method.
Information reference: /en/JavaScript/Reference/Global_Objects/String/replace
()grammar
(regexp|substr, newSubStr|function[, Non-standard flags]);
() Parameter description
regexp: A search regular expression
substr: a string for searching
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 (ignoring case, whether to search globally, matching multiple lines)
Specify a string as a replacement object
In the string used for replacement you can use the following pattern:
$$ => Insert a $
$& => Insert the matching substring
$` => Insert all characters before matching substring
$' => Insert all characters after matching substring
$n / $nn => This pattern is valid only if the first parameter of the replace() method is RegExp and the regular expression contains brackets.
Specify a function as a replacement object
Typical replacement function: function(str,p1,p2,offset,s){}
Parameter description:
str: matching string (similar to $&)
p1,p2,...: This pattern is valid only if the first parameter of the replace() method is RegExp and the regular expression contains brackets. (similar to $n/$nn)
offset: Match the offset of the substring
s: string used for search
Get the camel representation of CSS attribute
Copy the codeThe code is as follows:
=function(){
//all is the matching substring, while letter is p1, because [a-z] adds brackets
return (/-([a-z])/ig,function( all, letter,offset,s ) {
return ();
});
};
var cssText = 'h2\n{\n border-bottom:1px solid #eee;\n background-color:#bbb;\n}';
var newstr = ();
Swap the position of matching strings
Copy the codeThe code is as follows:
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = (re, "$2, $1");