-3

I have a string that looks like this s55d5bef032706_parameters_0_parameter and I want to replace the last _parameter with _value

How would I go about this?

Currently I have the line below which unfortunately replaces all occurrences with value

new_id = id.split('_parameter').join('_value');

The question asked enter link description here is a sentence while mine is a single word therefore solutions are different, I just need to replace the occurrence of '_parameter' in my word

Community
  • 1
  • 1
Masinde Muliro
  • 1,175
  • 3
  • 24
  • 38
  • 1
    possible duplicate of [JavaScript: replace last occurence of text in a string](http://stackoverflow.com/questions/2729666/javascript-replace-last-occurence-of-text-in-a-string) – JJJ Aug 20 '15 at 11:58
  • 3
    `new_id = id.replace(/_parameter$/,'_value');` – Dal Hundal Aug 20 '15 at 11:58
  • Thanks @DalHundal I think I tried this earlier why it didn't work beats. Maybe you need to add it as an answer so that I accept it – Masinde Muliro Aug 20 '15 at 12:01
  • @Juhana I saw that but remember the question there's different in the sense that the string is a sentence while mine is a single word – Masinde Muliro Aug 20 '15 at 12:22
  • 1
    Why on earth would that make any difference? – JJJ Aug 20 '15 at 12:23
  • [The accepted answer](http://stackoverflow.com/a/2729681) in the duplicate is *exactly the same* as Dal's solution, which you say works. – JJJ Aug 20 '15 at 12:28

1 Answers1

0

This must work:

new_id = id.replace(/_parameter$/,'_value');

Don't forget the $ symbol.

JomsDev
  • 116
  • 1
  • 9