-4
var string='url("images/bgtr.svg") top right no-repeat, url("images/bgbl.svg") bottom left no-repeat, url("images/overlay.png"), linear-gradient(45deg, #5f796b, #3a4e59, #2f394e);'

how to replace linear-gradient(45deg, #5f796b, #3a4e59, #2f394e) with different characters? it might not always be 'linear gradient'

1 Answers1

0

Use String.prototype.replace() - JavaScript | MDN

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.

I have assumed test as the different string in the code snippet below.

var string='url("images/bgtr.svg") top right no-repeat, url("images/bgbl.svg") bottom left no-repeat, url("images/overlay.png"), linear-gradient(45deg, #5f796b, #3a4e59, #2f394e);'

var replacedString = string.replace('linear-gradient(45deg, #5f796b, #3a4e59, #2f394e)', 'test');

console.log(replacedString);
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
  • Not enough regex! *it might not always be 'linear gradient'* – revo Jan 11 '17 at 18:33
  • @revo Thats what the OP has in the question. How do you know it may not always be 'linear gradient'? – Rahul Desai Jan 11 '17 at 18:35
  • If it might not be *linear-gradient* all the time, then it means it's not a static string therefore `replace` method shouldn't receive first argument as a static pattern. – revo Jan 11 '17 at 18:39
  • String.replace can take regex so something lije: `String.replace('[-\w]*\(\d+deg, #[a-z0-9]+, #[a-z0-9]+, #[a-z0-9]+\) ', 'blah')` – RSHAP Jan 11 '17 at 19:18
  • Although... Not entirely sure what u are doing but if your trying to read a webpage/html doc u should prob use an HTML parser instead of regex ... http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – RSHAP Jan 11 '17 at 19:30
  • @RSHAP I'm trying to replace one of the html values between commas with javascript. so no external parser. linear-gradient is the problematic value – Sludge Death Jan 11 '17 at 19:33