Computer >> Computer tutorials >  >> Programming >> Javascript

Capitalize a word and replace spaces with underscore - JavaScript?


For this, use trim() along with replace() and toLocaleUpperCase().

Example

Following is the code −

var sentence="my favourite subject is javascript.i live in us.";
var result=sentence.trim().replace(/ /g,'_').toLocaleUpperCase();
console.log(result);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo272.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo272.js
MY_FAVOURITE_SUBJECT_IS_JAVASCRIPT.I_LIVE_IN_US.