- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to replace leading zero with spaces - JavaScript
- How can I replace newlines with spaces in JavaScript?
- Using a recursive function to capitalize each word in an array in JavaScript
- How to capitalize the first letter of each word in a string using JavaScript?
- How to delete records based on a word with underscore like MONTH_JAN'?
- Capitalize last letter and Lowercase first letter of a word in Java
- How to replace multiple spaces with a single space in C#?
- C# program to replace all spaces in a string with ‘%20’
- Java Program to Replace the Spaces of a String with a Specific Character
- Write a Java program to capitalize each word in the string?
- Python Program to replace a word with asterisks in a sentence
- Java Program to replace a word with asterisks in a sentence
- Capitalize only the first letter after colon with regex and JavaScript?
- How to replace all occurrences of a word in a string with another word in java?
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript

Advertisements