【发布时间】:2011-11-24 12:31:20
【问题描述】:
目前我有这个匹配 RGB 字符串的正则表达式。我需要对其进行增强,使其足够强大以匹配 RGB 或 RGBA。
rgbRegex = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/; //matches RGB
var rgbString = "rgb(0, 70, 255)";
var RGBAString = "rgba(0, 70, 255, 0.5)";
var rgbRegex = /^rgb\((\d+),\s*(\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, 70, 255)", "0", "70", "255"]
console.log(partsRGBA); //null. I want ["rgb(0, 70, 255, 0.5)", "0", "70", "255", "0.5"]
【问题讨论】:
-
这会有很多误报。在逗号和百分比值之前允许使用空格而不是小数。
标签: javascript regex rgb rgba