function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to replace all occurances of any character in javascript.

Hi All,


I am trying to replace all occurances of a perticular string in javascript ,but the value is not getting change


Var str = 'Hello_world_congrats';

str = str.replace(/_/g,'&');


In this code i am trying to convert all '_' characters with '&' but the value is not getting change.



Can any one please let me know what's wrong in this.

Thanks in advance.




Best Answer chosen by Admin (Salesforce Developers) 


the below line is correct:

str = str.replace(/_/g,'&'); // there is no mistake.


The real mistake is, u need to write 'Var'  as var.because 'JavaScript is case sensitive'.

So u need to change the capital V to small v.


var str = 'Hello_world_congrats';
str = str.replace(/_/g,'&');


Please let me know if u have any problem in same and if this post helps u please throw KUDOS by click on star at left.