JavaScript

How to replace all character in a string in JavaScript

You can use the replace() method in JavaScript to replace the occurrence of a character in a string. However, the replace() method will only replace the first occurrence of the specified character. To replace all occurrences, you can use the global modifier (g).

The following example will show you how to replace all underscores (_) in a string with hyphens (-).
 

How to replace all character in a string in JavaScript
var str = 'welcom_to_stackhowto.com';

var str = str.replace(/_/g, "-");

alert(str);

Output:

welcom-to-stackhowto.com!
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *