I am running the following regex in JS to extract 3 R/G/B items from the string below which is rgba(r,g,b,a)
, but it's not working. I am getting the original string back.
var str = 'rgba(14, 48, 71, 0.3)';
/* Goal: rgb(14,48,71) */
console.log (str.replace(/^rgba\((\d+),\s*(\d+),\s*(\d+),(\d+.\d+)\)$/, 'rgb($1,$2,$3)'));
I am running the following regex in JS to extract 3 R/G/B items from the string below which is rgba(r,g,b,a)
, but it's not working. I am getting the original string back.
var str = 'rgba(14, 48, 71, 0.3)';
/* Goal: rgb(14,48,71) */
console.log (str.replace(/^rgba\((\d+),\s*(\d+),\s*(\d+),(\d+.\d+)\)$/, 'rgb($1,$2,$3)'));
\s*
in front of your last capture group.
– jmcgriz
Commented
Apr 4, 2018 at 15:55
rgba()
expression? If so, you're going to have to consider valid expressions like rgba(1e2, .5e1, .5e0, +.25e2%)
.
– Jordan Running
Commented
Apr 4, 2018 at 16:00
rgba?\((\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*)(((?:,\s*[0-9.]*\s*)?))\)
Was missing the third group
– nnamdi
Commented
Apr 4, 2018 at 16:04
0
in the ones place (i.e. .3
for 0.3
), or to see just 0
or 1
. None of those cases are covered by OP's original regexp. Finally, CSS isn't just written by people. There are lots of tools that generate CSS code, and CSS color values in particular, and we can't make assumptions about what number format(s) they'll use.
– Jordan Running
Commented
Apr 4, 2018 at 16:14
You could also write a more consolidated version of the regex like this:
var str = 'rgba(14, 48, 71, 0.3)';
/* Goal: rgb(14,48,71) */
var rgx = /^rgba\(((,?\s*\d+){3}).+$/
console.log (str.replace(rgx, 'rgb($1)'));
It's much easier to extract the numbers and rebuild the string rather than trying to remove all the parts you don't want.
var str = 'rgba(14, 48, 71, 0.3)';
var [r,g,b] = str.match(/[\d\.]+/g);
var rgb = `rgb(${r}, ${g}, ${b})`;
console.log(rgb)
You had errors in your expression:
.
character
var str = 'rgba(14, 48, 71, 0.3)';
/* Goal: rgb(14,48,71) */
var regex = /^rgba\((\d+),\s*(\d+),\s*(\d+),\s*(\d+\.\d+)\)$/;
console.log (str.replace(regex, 'rgb($1,$2,$3)'));
Alternative solution:
var answer = "rgb(" + str.split(",")[0].match(/\d+/)[0] + "," + str.split(",")[1] + "," + str.split(",")[2] + ")";
If you want to match a wider range of color and alpha values (e.g. 30%
, .0
, .5e10
, all of which are valid), you'll need to be a bit looser with your regular expression. Consider:
/\brgba\((.+?),[^,]+?\)/g
This will match any rgba(R, G, B, A)
expression and capture all of the argument except the last. The JavaScript replace
call would look like this:
str.replace(/\brgba\((.+?),[^,]+?\)/g, 'rgb($1)')
You can see it in action in the below snippet:
const MATCH_CSS_RGBA = /\brgba\((.+?),[^,]+?\)/g;
const MATCH_CSS_RGBA_REPLACEMENT = 'rgb($1)';
function replaceRgbaWithRgb(input) {
return input.replace(MATCH_CSS_RGBA, MATCH_CSS_RGBA_REPLACEMENT);
}
/*** the below is just for demonstration purposes ***/
const [input, output] = document.querySelectorAll('textarea');
function testReplace() {
output.value = replaceRgbaWithRgb(input.value);
}
input.addEventListener('input', testReplace);
testReplace(input);
textarea{display:block;font-family:monospace;width:80%;height:80px;white-space:pre}
In (edit me!):
<textarea>i.cat1{background:rgb(249, 115, 0);} /* RGB with 3 params */
i.cat2{background:rgba(14, 48, 71, 0.99);} /* RGBA with 4 params */
i.cat3{background:rgba(1e2, .5e1, .5e0, +.25e2%);} /* ~exotic numbers~ */</textarea>
Out:
<textarea disabled></textarea>
...or on Regex101: https://regex101./r/6mZDuC/1