正则表达式javascript匹配RGB和RGBA

前端之家收集整理的这篇文章主要介绍了正则表达式javascript匹配RGB和RGBA前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目前我有这个与RGB字符串匹配的正则表达式.我需要它增强,以便它足够强大,以匹配RGB或RGBA.
rgbRegex = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/; //matches RGB

http://jsfiddle.net/YxU2m/

var rgbString =  "rgb(0,70,255)";
var RGBAString = "rgba(0,255,0.5)";

var rgbRegex = /^rgb\((\d+),\s*(\d+)\)$/;
//need help on this regex
//I figure it needs to be ^rgba?,and then also an optional clause to handle the opacity

var partsRGB = rgbString.match(rgbRegex);
var partsRGBA = RGBAString.match(rgbRegex);

console.log(partsRGB); //["rgb(0,255)","0","70","255"]
console.log(partsRGBA); //null. I want ["rgb(0,0.5)","255","0.5"]
这会吗?
var rgbRegex = /^rgba?\((\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/
原文链接:https://www.f2er.com/regex/357286.html

猜你在找的正则表达式相关文章