Chuỗi có từ viết hoa đầu tiên (sau khoảng trắng). Thêm dấu phẩy trước khoảng trắng đứng trước từ viết hoa.
Ví dụ: "Connecting to server Connection has been successful We found result" → "Connecting to server, Connection has been successful, We found result".
Mã thực hiện
const str = "Connecting to server Connection has been successful We found result";
const capitaliseNew = str => {
let newStr = '';
const regex = /.[A-Z]/g;
newStr = str.replace(regex, ',$&');
return newStr;
};
console.log(capitaliseNew(str));
Kết quả
Connecting to server, Connection has been successful, We found result