简体   繁体   中英

Regex based search and replace in vscode

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 eg 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.

To match with any amount of whitespace around the numbers:

Search: rgb(?=\((\s*\d+\s*,){3}\s*\d+\s*\))
Replace: rgba 
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM