Given that I have the following string
string= "goal01=4 goal02=2 goal06=3 goal09=1 goal12=5 goal13=2 goal14=4 planet=52 people=48 Inclusion=4 HealthWellness=2 SustainableInfrastructure=4 ResponsibleConsumption=9 Environment=2"
var stringReplace = string.replace(/goal/g, "iGoal").replace(/planet/g, "iPlanet").replace(/Environment/g, "iEnvironment").replace(/HealthWellness/g, "iHealthWellness").replace(/SustainableInfrastructure/g, "iSustainableInfrastructure").replace(/ResponsibleConsumption/g, "iResponsibleConsumption");
My replace code works, but I think it can be improved upon, so if I want to create an object such as;
stringReplace =
{
items:
{
"goal":"iGoal",
"planet":"iPlanet",
"people":"iPeople",
"Inclusion":"iInclusion",
"Environment":"iEnvironment",
"HealthWellness":"iHealthWellness",
"SustainableInfrastructure":"iSustainableInfrastructure",
"ResponsibleConsumption":"iResponsibleConsumption"
}
};
what would be the equivalent of the .replace() function in this case?
abcandAbcat the start of a word withiAbc. In the latter case you could just dostring.replace(/\b[a-z]/gi, c => 'i' + c.toUpperCase()).