Search code examples
javascriptregexreplacergba

Replacing\Changing Alpha in RGBA JavaScript


I'm trying to change the Alpha in RGBA, from some reason replace acts strange and instead of resulting with: "rgba(30, 43, 2, A)" i end up with: "A".

Here is the code:

var color="rgba(30, 43, 2, 0.498039)";
color = color.replace(/^.*,(.+)\)/gi,"A");
alert(color);

JS Fiddle Demo


Solution

  • I don't get your logic but you can do this :

    color = color.replace(/[\d\.]+\)$/g, 'A)')
    

    What it matches :

    • [\d\.]+ : a combination of . and digits
    • \) : the end parenthesis
    • $ : the end of the string (you can remove it if you don't want to ensure it's the end of the string)