Change the string in javascript

I want to change the color attribute that is returned in jQuery. So let's assume that the color is returned and contained in

var color
color = 'rgb(148, 141, 124)'

I want to change the color value:

color = 'rgb(148, 141, 124, .7)'

(in other words, insert the string ", .7")

+4
source share
4 answers

You can do the following:

color = color.replace(/\)/, ', 0.7)')
+2
source

Try

var color = 'rgb(148, 141, 124)';
var newColor = color.slice(0,-1) + ",.7)"

Demo

If you want it to be rgba use

var color = 'rgb(148, 141, 124)';
var newColor = (color.slice(0,-1) + ",.7)").split('(').join('a(');

Demo

+5
source

, :

var color = 'rgb(148, 141, 124)';
var colorAlpha = color.replace(/rgb/g, 'rgba').replace(/\)/g, ', 0.7)');

FIDDLE

, - , :

var color = 'rgba(148, 141, 124, 1.0)';
var colorAlpha = color.replace(/1.0/g, '0.7');
alert(colorAlpha);
+1

.split(), .

var color = 'rgb(148, 141, 124)';
var newColor = color.split(")")[0];

alert(newColor + ', 0.7)');

DEMO

0

Source: https://habr.com/ru/post/1546238/


All Articles