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 '*'. '.' ...
随机推荐
- Hash Array Mapped Trie
Hash Array Mapped Trie Python\hamt.c
- functools.singledispatchmethod(Python 3.8) | 码农网 https://www.codercto.com/a/83245.html
functools.singledispatchmethod(Python 3.8) | 码农网 https://www.codercto.com/a/83245.html
- (006)每日SQL学习:关于to_char函数
to_char函数的官方文档说明: 详细to_char请移步:https://www.cnblogs.com/reborter/archive/2008/11/28/1343195.html 需求:n ...
- LOJ10077
题目描述给出一个 N 个顶点 M 条边的无向无权图,顶点编号为 1∼N.问从顶点 1 开始,到其他每个点的最短路有几条. 输入格式第一行包含 2 个正整数 N,M,为图的顶点数与边数. 接下来 M行, ...
- Language Guide (proto3) | proto3 语言指南(七)更新消息类型
Updating A Message Type - 更新消息类型 如果现有的消息类型不再满足您的所有需要(例如,您希望消息格式有一个额外的字段),但是您仍然希望使用用旧格式创建的代码,不要担心!在不破 ...
- Redis,JedisPool工具类
Redis,JedisPool工具类 1.JedisPool 详细配置解释代码 2.Jedis工具类 导入相关依赖: commons-pool2-2.3.jar jedis-2.7.0.jar 1.J ...
- Kafka客户端Producer与Consumer
Kafka客户端Producer与Consumer 一.pom.xml 二.相关配置文件 producer.properties log4j.properties base.properties 三. ...
- 查看linux系统 公网ip
查公网IP时候, 百度输入 IP可看 linux 系统(无界面) : curl cip.cc
- 将文件转成byte[]文件属组
/** * * @Description : 读取文件数组 * @Method_Name : fileBuff * @param filePath * @return * @throws IOExce ...
- Git实现1个项目2个地址1次推送
Git实现1个项目2个地址1次推送 考虑到不需要pull操作,因此本方法适用于个人项目分别在两个平台或地址进行部署 给origin 增加一个可以push的地址 git remote set-url - ...