- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
How to find capitalized words and add a character before that in a given sentence using JavaScript?
Suppose, we have a string that contains some capitalized English alphabets like this −
const str = "Connecting to server Connection has been successful We found result";
We are required to write a JavaScript function that takes in one such string and inserts a comma ',' before the space before every capital letter in the string.
The code for this will be −
const str = "Connecting to server Connection has been successful We found result"; const capitaliseNew = str => { let newStr = ''; const regex = new RegExp(/.[A-Z]/g); newStr = str.replace(regex, ',$&'); return newStr; }; console.log(capitaliseNew(str));
Following is the output on console −
Connecting to server, Connection has been successful, We found result
Advertisements