0

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?

3
  • 1
    there is a difference between your examples, in the first one there is no replace for people->iPeople Commented Jul 4, 2021 at 9:35
  • Just want to make sure before writing an answer: there is also missing inclusion -> iInclusion. Commented Jul 4, 2021 at 9:39
  • 2
    It's not clear if you want to specifically do it this way (or even replace these variables) or if you maybe just want to replace any abc and Abc at the start of a word with iAbc. In the latter case you could just do string.replace(/\b[a-z]/gi, c => 'i' + c.toUpperCase()). Commented Jul 4, 2021 at 9:42

2 Answers 2

1

I agree with @CherryDT that your example is not very clear. Here are some answers for different interpretations.

Interpretation 1

If you want to use a predefined list of "replacement instructions" you could go with a loop over each element of your instructions.

Object.keys(stringReplace.items).forEach(x => string = string.replaceAll(x, stringReplace.items[x]));

Interpretation 2

If you want to generically i-prefix + uppercase specific words, you could use:

string = string.replace(new RegExp("(" + Object.keys(stringReplace.items).join("|") + ")", "g"), s => "i" + s[0].toUpperCase() + s.slice(1));

You could use an array instead of the stringReplace object in that case because the values are not used here.

Interpretation 3

If you know which words potentially exist in the string and you want to replace all of them with their i-prefix + uppercase equivalent, you can use (CherryDT's answer):

string = string.replace(/\b[a-z]/gi, c => 'i' + c.toUpperCase())
Sign up to request clarification or add additional context in comments.

Comments

0

Your logic is a little unclear, but it looks like you want to discard the multiple numbered elements, ('goal01', 'goal02', etc) as well as the values.

That being the case, you can simply match all words excluding digits and symbols, and simplify your multiple replace() calls to a single replace adding an 'i' and making the first letter uppercase, then reducing the results to an object.

const
  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",
  object = string
    .match(/[a-zA-Z]+/g)
    .reduce((a, s) => ({ ...a, [s]: `i${s.replace(/^./, m => m.toUpperCase())}` }), {});

console.log(object);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.