0

I need to edit a string from:

rgb(250, 0, 0) 10px 10px 50px 0px

to:

rgba(250, 0, 0, 1) 10px 10px 50px 0px

Assuming the input value is always correct, I need to add alpha channel of 1.

The follow script I created works, but I was wondering about the double replace.

I would like to know if you perform a better way, maybe using only one regex and avoiding using replace twice or a better alternative.

        var _convertBoxShadowToTgba = function (boxShadow) {
            return boxShadow.replace(/rgb/i, 'rgba').replace(/\)/i, ', 1)');
        };
        var original = "rgb(250, 0, 0) 10px 10px 50px 0px";
        var final = _convertBoxShadowToTgba(original);
        console.log('original', original);
        console.log('final', final);

3 Answers 3

2

Regex:

rgb(.*?\d)\)

Explanation:

rgb          # Match literal `rgb`
    (        # Start of capturing group (1)
        .*?  # Match every thing lazily
        \d   # Up to a digit
    )        # End of capturing group (1)
\)           # That is followed by a closing bracket

Replacement string:

rgba$1, 1)

$1 is a back-reference to first capturing group

JS code:

original.replace(/rgb(.*?\d)\)/i, 'rgba$1, 1)')

Live demo

Sign up to request clarification or add additional context in comments.

3 Comments

The battle for rep on simple regexp questions is endless :)
When two answers are posted accidentally in a moment, improving one to promote a better understating of solution is better than removing it. @xShirase
@revo I totally agree, that is what makes SO great, the diversity of explanations and approaches. I often find valuable information within more than one answer. Congrats on a great post
0

Use groups, like this :

/rgb(.*)\)/

this will group everything between rgb and the last parenthesis. You can then reuse the group within your replace expression :

.replace(/rgb(.*)\)/, 'rgba$1, 1')

Test :

a = 'rgb(250, 0, 0) 10px 10px 50px 0px'

a.replace(/rgb(.*)\)/i, 'rgba$1, 1)')
>"rgba(250, 0, 0, 1) 10px 10px 50px 0px"

The way it works is as follows :

the expression between parenthesis is called a group, you can reuse it using $ variables in your replace expression.

For example :

a = 'Hello, foo!'
a.replace(/(Hello)(.*)(foo)/, '$3$2$1')
> foo, Hello!

In this example, groups are :

$1 : (Hello)
$2 : (.*)
$3 : (foo)

You can switch them around, replace them, etc, as you wish.

Further reading on regex grouping

Comments

0

You can also pass a function to the replace method. Like this:

var _convertBoxShadowToTgba = function (boxShadow) {
        return boxShadow.replace(/rgb|\)/g, function($0) {
                 if ($0 == 'rgb') { return 'rgba';}
                 if($0 == ')') {return ', 1)';}
            });
 };
 var original = "rgb(250, 0, 0) 10px 10px 50px 0px";
 var final = _convertBoxShadowToTgba(original);
 console.log('original', original);
 console.log('final', final);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.