0
votes

In VSCode: I would like to do a wildcard replace of:

rgb(1, 1, 1 ,1)

with:

rgba(1, 1,1 ,1)

Essentially when an alpha value is specified, the datatype should be changed from "rgb" to "rgba". Where alpha is not specified e.g rgb(1,1,1) - they should remain unchanged.

I tried:

Find: rgb(.*,.*,.*,.*) Replace: rgba($1)

which obviously did not work. What would be the correct regex syntax to achieve this? Thank you.

Update: Please note that some locations there are spaces before/after commas. Not consistent.

2
Valid RGB values are 0-9 and a-f or what? - user3783243
valid RGBA values are 0 through 255. Thanks - SingularRiver
Maybe rgb\((\d+,\d+,d+,\d+)\) would do it in that case. (That's a bit looser than 0-255 but you can change \d to a range if that works (e.g. stackoverflow.com/questions/31684083/…)) - user3783243
Loose is fine. But it did not work for me. VSCode search did not recognize any text with that pattern (which I know it should have). Thanks. - SingularRiver
activate the regex search with the .* button in the find box - rioV8

2 Answers

1
votes

To match with any amount of whitespace around the numbers:

Search: rgb(?=\((\s*\d+\s*,){3}\s*\d+\s*\))
Replace: rgba 
0
votes
Search: rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)
Replace: rgba($1, $2, $3, $4)

This will match any amount of whitespace and will fix them after replace.