मान लीजिए हमारे पास एक स्ट्रिंग है जिसमें कुछ बड़े अंग्रेजी अक्षर हैं:
const str = "Connecting to server Connection has been successful We found result";
हमें एक JavaScript फ़ंक्शन लिखना है जो ऐसी स्ट्रिंग लेता है और स्ट्रिंग में प्रत्येक बड़े अक्षर से पहले स्पेस के पहले कॉमा ',' डालता है।
इसके लिए कोड:
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));
कंसोल पर आउटपुट:
Connecting to server, Connection has been successful, We found result