admin管理员组文章数量:1429061
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:
using the code that was provided, I was able to change them all to their full name.
Thank you thank you thank you!
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./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!
Share Improve this question edited Apr 11, 2018 at 4:16 wp78de 19k7 gold badges46 silver badges77 bronze badges asked Apr 10, 2018 at 15:03 PepperAddictPepperAddict 1,0282 gold badges11 silver badges19 bronze badges 2-
Object.keys(state).forEach(k => str = str.replace(k, state[k]))
– Ele Commented Apr 10, 2018 at 15:05 -
You live in ${arrayOfStates['NY']}
– Joe Warner Commented Apr 10, 2018 at 15:07
4 Answers
Reset to default 2You 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);
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())
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))
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"
本文标签: javascriptHow to use regex to match object key and replace with its valueStack Overflow
版权声明:本文标题:javascript - How to use regex to match object key and replace with its value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745543622a2662604.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论