Regular Expression & rgb2hex

regex

// 颜色字符串转换
function rgb2hex(sRGB = 'rgb(255, 255, 255)') {
const temp = sRGB;
// /^rgb\([\s*\d+,]{2}[\s*\d+]\)$/
if(/^rgb\([\s*\d+,]{2}[\s*\d+]{1}\)$/.test(temp)) {
// 符合 rgb 格式
} else {
// 输入不符合 rgb 格式,返回原始输入
return temp;
}
}

function rgb2hex (sRGB = 'rgb(255, 255, 255)') {
const regex = /^(rgb|RGB)\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)/;
if(!regex.test(sRGB)){
return sRGB;
}else {
function hex(str) {
// string < number ️
return str < 16 ? "0" + Number(str).toString(16) : Number(str).toString(16);
}
return sRGB.replace(regex, function(a, c, r, g, b){
console.log(`a, c`, a, c);
console.log(`r, g, b`, r, g, b);
console.log(`typeof r, typeof g, typeof b`, typeof r, typeof g, typeof b);
// console.log(`a, c, r, g, b`, a, c, r, g, b);
return "#" + hex(r) + hex(g) + hex(b);
})
}
} function rgb2hex (sRGB = 'rgb(255, 255, 255)') {
const rgbStr = sRGB.toLowerCase();
// rgb 50%, RGB 50%
const regex = /^(rgb|RGB)\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)/;
// const regex = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)/;
if(!regex.test(rgbStr)){
return sRGB;
}else {
function hex(str) {
return str < 16 ? "0" + Number(str).toString(16) : Number(str).toString(16);
}
return rgbStr.toLowerCase().replace(regex, function(a, c, r, g, b){
console.log(`a, c`, a, c);
console.log(`r, g, b`, r, g, b);
// console.log(`a, c, r, g, b`, a, c, r, g, b);
return "#" + hex(r) + hex(g) + hex(b);
})
}
}

refs

https://regexper.com/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


Regular Expression & rgb2hex的更多相关文章

  1. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  2. myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc

    今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...

  3. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  4. No.010:Regular Expression Matching

    问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...

  5. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  7. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. grep(Global Regular Expression Print)

    .grep -iwr --color 'hellp' /home/weblogic/demo 或者 grep -iw --color 'hellp' /home/weblogic/demo/* (-i ...

  9. 66. Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

随机推荐

  1. code-server Command ' ' not found

    由于通过一些特殊的方式登录linux用户后,全局变量不会自动加载,需要在 vscode 的 bash terminal手动读取 输入 source /etc/profile 或者vim ~/.bash ...

  2. CPU飙高,系统性能问题如何排查?

    CPU飙高,系统性能问题如何排查? 原创 雍雍 阿里技术 2020-09-29 https://mp.weixin.qq.com/s/fzLcAkYwKhj-9hgoVkTzaw  

  3. 自动化接口差异测试-diffy 回归测试 charles rewrite 请求

    https://mp.weixin.qq.com/s/vIxbtQtRRqgYCrDy7XTcrA 自动化接口差异测试-diffy Boris 搜狗测试 2018-08-30   自动化接口差异测试- ...

  4. Java关键字及作用解释

    访问控制 1) private 私有的 private 关键字是访问控制修饰符,可以应用于类.方法或字段(在类中声明的变量). 只能在声明 private(内部)类.方法或字段的类中引用这些类.方法或 ...

  5. SSM、SSH框架搭建,面试点总结

    文章目录 1.SSM如何搭建:三个框架的搭建: 2.SSM系统架构 3.SSM整合步骤 4.Spring,Spring MVC,MyBatis,Hibernate个人总结 5.面试资源 关于SSM.S ...

  6. PHP-mysql存储照片的两种方式

    PHP-mysql存储照片的两种方式 方式一:把图片数据存储在数据库中(二进制) 数据库代码: CREATE TABLE `photo` (    `id` int(10) unsigned NOT ...

  7. 微信支付V3 SDK Payment Spring Boot 1.0.6 发布,实现留守红包,助力抗疫

    春节将至,为防控疫情,多地政府提倡员工.外来务工者留守本地过年,并鼓励企业向员工发放"留守红包".为此,微信支付推出"春节留守红包"能力,希望可以协助有发放需求 ...

  8. 从云数据迁移服务看MySQL大表抽取模式

    摘要:MySQL JDBC抽取到底应该采用什么样的方式,且听小编给你娓娓道来. 小编最近在云上的一个迁移项目中被MySQL抽取模式折磨的很惨.一开始爆内存被客户怼,再后来迁移效率低下再被怼.MySQL ...

  9. 2015 German Collegiate Programming Contest (GCPC 15) + POI 10-T3(12/13)

    $$2015\ German\ Collegiate\ Programming\ Contest\ (GCPC 15) + POI 10-T3$$ \(A.\ Journey\ to\ Greece\ ...

  10. 【NOIP 2015 D1 T2】信息传递(图论--带权并查集/bfs)

    题目:有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学.游戏开始时,每人都只知道自己的生日.之后每一轮中, ...