I need to replace all the string in a variable.
var a = "::::::";
a = a.replace(":", "hi");
console.log(a);
The above code replaces only the first string i.e..hi::::::
I used replaceAll but it’s not working.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Update: All recent versions of major browsers, as well as NodeJS 15+ now support
replaceAllOriginal:
There is no
replaceAllin JavaScript: the error console was probably reporting an error.Instead, use the
/g("match globally") modifier with a regular expression argument toreplace:The is covered in MDN: String.replace (and elsewhere).