ใช้ replace() กับ Regex /([:?]\s+)(.)/g และฟังก์ชัน Callback ให้ toUpperCase()
ตัวอย่างโค้ด
var objectValues = 'fullName: john Smith';
function capitalizeFirstAfterTheColon(value) {
return value.replace(/([:?]\s+)(.)/g, function(data) {
return data.toUpperCase();
});
}
console.log(capitalizeFirstAfterTheColon(objectValues));
วิธีรันโปรแกรม
ใช้คำสั่ง node fileName.js (เช่น node demo85.js)
ผลลัพธ์
fullName: John Smith