.replace(/rgba(0,0,0,0.87)/g,newPrimaryTextColor)
Is not working. Have any other option?
I am trying to get replace old css with new css via jquery. CSS file get via jquery.
.replace(/rgba(0,0,0,0.87)/g,newPrimaryTextColor)
Is not working. Have any other option?
I am trying to get replace old css with new css via jquery. CSS file get via jquery.
If you are looking for a way to replace an existing text color definition by a new one then maybe the following might help you:
$('span').filter(function() { // span,div,td ... or other desired elements
var match = 'rgb(0, 0, 0)'; // match text-color: black
/*
true = keep this element in our wrapped set
false = remove this element from our wrapped set
*/
return ( $(this).css('color') == match );
}).css('color', newPrimaryTextColor); // actually change the color
This is an adaptation of a previous question Selecting elements with a certain background color and it will only change those elements with an explicit color definition of rgb(0, 0, 0)
on it. A better approach might be to work with classes instead, but I can only guess ...
The issue is the regular expression is failing to escape the (starting and ending of the string ). You need to add separate logic to escape these characters:
function escapeSpecialChars(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
Now specify your method as specified below:
function ReplaceColor() {
var textContent = "Replace rgba(0,0,0,0.87) with red";
var expression = new RegExp(escapeSpecialChars("rgba(0,0,0,0.87)"));
var res = textContent.replace(expression, 'red');
}
Please refer Is there a RegExp.escape function in Javascript? for more details