1

I'm still new with Javascript and I'm not entirely sure how to go about this.

I want it to match the key field and replace it with its value.

const state = {
"NY": "New York"
}

You live in NY to You live in New York

The list is going to be quite long with all the states so I think I'll have to use objects. Any help would be appreciated! Thank you in advance!

EDIT>>>

Thank you so much to those that replied! I made a list of all the states for USA, Canada, and Mexico to their full name here: https://gist.github.com/PepperAddict/b8c6c80af4a17908fd98378b4375047e

using the code that was provided, I was able to change them all to their full name.

Thank you thank you thank you!

2
  • Object.keys(state).forEach(k => str = str.replace(k, state[k])) Commented Apr 10, 2018 at 15:05
  • You live in ${arrayOfStates['NY']} Commented Apr 10, 2018 at 15:07

4 Answers 4

2

You don't need regex, use the keys from the object state

Object.keys(state).forEach(k => str = str.replace(k, state[k]));

const state = { "NY": "New York" };
var str = "You live in NY";

Object.keys(state).forEach(k => str = str.replace(k, state[k]));
console.log(str);

Using regex to replace the whole set of matches:

const state = { "NY": "New York" };
var str = "You live in NY and again in NY";

Object.keys(state).forEach(k => str = str.replace(new RegExp(`\\b${k}\\b`, 'g'), state[k]));
console.log(str);

Sign up to request clarification or add additional context in comments.

3 Comments

Wow. This worked beautifully!!! I had a feeling I was overcomplicating this. Thank you!!!
Only downside here is that it will only work with a single instance of each state. If it shows up more than once, the later instances will not be replaced. For that, you either need a while loop or regex
I've added an answer that shows the regex solution that will handle all replacements
1

We can make use of template literals and map it will return an array which you can then do what you want with.

const state = {
"NY": "New York"
}
console.log(Object.keys(state).map(s => `You live in ${state[s]}`))

If you're planning to do this with a user for example


const state = {
"NY": "New York"
}
const user = {
 name: "joe",
 state: "NY",
 liveIn: () => `You live in ${state[user.state]}`
}
console.log(user.liveIn())

Comments

1

const states = {
  "CA": "California",
  "NY": "New York"
}

var input = "I'm from CA, thinking of moving to NY but not sure, I still really like CA."

var regexStr = Object.keys(states).join("|")
var statesRgx = new RegExp(`\\b(${regexStr})\\b`, 'g')
console.log(statesRgx)
function stateReplace(str){
  return str.replace(statesRgx, val => states[val])
}

console.log(stateReplace(input))

Comments

1

if you are sure the last word is key. Then you can skip foreach by following.

const state = {
"NY": "New York"
}

var a = "You live in NY";

b = a.split(" ");

b = b[b.length - 1];


a.replace(b, state[b]);

Console.log(a); 

Output

"You live in New York"

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.