目前我有这个与RGB字符串匹配的正则表达式.我需要它增强,以便它足够强大,以匹配RGB或RGBA.
rgbRegex = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/; //matches RGB
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+)?))?\)$/