*
Converts an RGB color string to a 16-binary form, such as RGB (255, 255, 255) to #ffffff
1. Each in RGB, the number of spaces behind is not fixed
2.16 binary expressions use six-bit lowercase letters
3. If the input does not conform to the RGB format, return the original input
Input: ' RGB (255, 255, 255) '
Output: #ffffff
function Rgb2hex (SRGB) {var int2hex = function (n, W) {var m, s = [], c;w = W | | 2;while (n) {m = n% 16;if (ten <= m) {C = String.fromCharCode (97+m-10);} else {c = "" + M;} S.unshift (c); n = Math.floor (N/16);} var p = w-s.length; Length to pad ' 0 ' while (p--) {s.unshift (' 0 ');} Return S.join (");};/ *var trim = function (s) {return s.replace (/(^\s*) | ( \s*$)/g, "");}; */ return Srgb.replace (/^rgb\ ((\d+) \s*,\s* (\d+) \s*,\s* (\d+) \) $/, function ($, $, $, $ ) { return ' # ' + Int2hex ($) + Int2hex ($) + Int2hex ($); }); Console.log (Rgb2hex (' RGB (255, 255, 255) '); #ffffffconsole. Log (Rgb2hex (' RGB (103, 111) '); #5a676fconsole. Log (Rgb2hex (' RGB (0,0,15) ')); #00000f
10 binary integers converted to 16 binary 2-bit
(' 0 ' + (+str). toString ()). Slice (-2)
function Rgb2hex (SRGB) {var int2hex = function (str) {return (' 0 ' + (+STR). toString). Slice ( -2);}; Return Srgb.replace (/^rgb\ (\d+) \s*,\s* (\d+) \s*,\s* (\d+) \) $/, function ($, $, $, $ ) { return ' # ' +int2hex ( $) + Int2hex ($) + Int2hex ($); });
* String converted to hump form
CSS often has similar background-image-connected characters, through JavaScript to set the style of this style need to be converted to BackgroundImage hump format, please complete this conversion function
1. The first letter of the second non-empty word is capitalized as a delimiter
2. The result of-webkit-border-image conversion is webkitborderimage
function Cssstyle2domstyle (sName) {var ucfirst = function (s, delim) {Delim = Delim | | ‘-‘; Return S.split (Delim). Map (function (s) {var c = s.charcodeat (0); if (<= c && C < + +) {return s; } if (<= C && C < + +) {c = c & 0xdf; } return String.fromCharCode (c) + S.SUBSTR (1); }). Join ('); } var lowerfirstletter = function (s) {var i = 0, c = s.charcodeat (i); while (I < s.length) {if (<= C && C < + +) {return s; } if (<= C && C < + +) {c = c | 0x20; Break } else {c = s.charcodeat (++i); }} return String.fromCharCode (c) + s.substr (i+1); } var s = Ucfirst (sName); return Lowerfirstletter (s);} Console.log (CSSStyle2DomStyle ("-webkit-border-image")); WebkitBorderImageconsole.log (Cssstyle2domstyle ("font-size")); FontSizeconsole.log (Cssstyle2domstyle ("--selection")); Selection
//REGEXP
Link: https://www.nowcoder.com/questionTerminal/2ded24e34ec34325a62d42d0c8479bae//Source: Niu ke network function Cssstyle2domstyle (sName) {return sname.replace (/(?! ^) \-(\w) (\w+)/g, function (A, B, c) {return b.touppercase () + c.tolowercase (); }). replace (/^\-/, ');}
CSS color string conversion, string conversion to camel form