Regular Expression & rgb2hex
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://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的更多相关文章
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc
今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- No.010:Regular Expression Matching
问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- grep(Global Regular Expression Print)
.grep -iwr --color 'hellp' /home/weblogic/demo 或者 grep -iw --color 'hellp' /home/weblogic/demo/* (-i ...
- 66. Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
随机推荐
- jQuery 点击input框标题收起
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 下面给出一个child-parent的表格,要求挖掘其中的父子辈关系,给出祖孙辈关系的表格。
package org.apache.hadoop.examples; import java.util.HashMap; import java.io.IOException; import jav ...
- 每天学一点 Vue3(一) CND方式的安装以及简单使用
简介 感觉vue3的新特性很舒服,这样才是写软件的感觉嘛.打算用Vue实现自己的一些想法. Vue3还有几个必备库,比如Vue-Router(负责路由导航).Vuex(状态管理.组件间通信),还有第三 ...
- “batteries included” philosophy
https://docs.djangoproject.com/en/2.2/ref/contrib/ contrib packages Django aims to follow Python's & ...
- the minimum number of bits required to represent x 最小位数
src/math/bits/bits.go:296 // --- Len ---// Len returns the minimum number of bits required to repres ...
- Jenkins部署springboot项目
记录jenkins如何部署springboot项目(jar类型的) 一.首先需要先配置好jenkins的基本配置(jdk.maven--),可在系统管理-->>全局工具配置中进行配置. 配 ...
- MySql(二)索引的设计与使用
MySql(二)索引的设计与使用 一.索引概述 二.设计索引的原则 三.BTREE索引与HASH索引 一.索引概述 所有Mysql列类型都可以被索引,对相关列使用索引时提高select操作性能的最佳途 ...
- Prometheus监控Kafka
Prometheus监控Kafka 1.Prometheus监控Kafka,Docker方式 Kafka监控优秀博文: 简书:whaike:[监控]Kafka - 详细指标 CSDN:GeekXuSh ...
- multiselect多选下拉框
具体实现 <input type="hidden" id="q_dueDay" name="q_dueDay" value=" ...
- (7)Linux使用注意事项
1.Linux 严格区分大小写 和 Windows 不同,Linux 是严格区分大小写的,包括文件名和目录名.命令.命令选项.配置文件设置选项等. 2.Windows 下的程序不能直接在 Linux ...