替换字符串中找到的单词
Replace words found in string
我正在尝试用匹配的键和索引位置替换字符串中的单词而不删除其他单词。
我想要的输出是:嘿嘿嘿你好
到目前为止我已经尝试过...
var data = {
items: ["item", "HI", "item2"],
moreitems: ["moreitem", "moreitem1", "HELLO"]
}
var str = "hey #items1 hey #moreitems2";
var newStr = '';
var match = str.match(/#(.*?)\d+/g); //get str that starts with # and ends with digits
for (var i = 0; i < match.length; i++) {
var keys = match[i].replace(/#|\d+/g, ''), // remove hash and numbers to match the data keys
pos = match[i].replace(/\D/g, ''); // get the digits in str
newStr += data[keys][pos] + ' ';
}
console.log(newStr)
感谢您的帮助!
你可以使用
- string.replace替换进程末尾的substr
- 通过
match[match.length - 1]
恢复匹配字符串的最后一个字符来计算字符串数组中的索引
- 通过删除第一个和最后一个字符恢复要使用的数据类别
category = match.slice(0, -1).substring(1);
var data = {
items: ["item", "HI", "item2"],
moreitems: ["moreitem", "moreitem1", "HELLO"]
}
var str = "hey #items1 hey #moreitems2";
var newStr = str;
var match = str.match(/#(.*?)\d+/g);
var index, category;
console.log(match);
match.forEach(match => {
index = match[match.length - 1];
category = match.slice(0, -1).substring(1);
newStr = newStr.replace(match, data[category][index]);
});
console.log(newStr)
const dic = {
xxx: 'hello',
yyy: 'world',
zzz: 'peace'
}
const string = 'hey! #xxx, #yyy! #zzz on you'
const newString = string.replace(/#\w+/g, item => dic[item.substring(1)])
console.log(string)
console.log(newString)
一个简单的解决方案是将 replace()
与替换函数一起使用。
我使用正则表达式 /#(\D+)(\d+)/g
。匹配 #
,后跟一个或多个 non-digits(放置在捕获组 1 中),后跟一个或多个数字(放置在捕获组 2 中)。
所有捕获组都作为替换函数的参数传递。完整匹配作为第一个参数传递,捕获组 1 作为第二个参数传递,依此类推。
在替换函数中,您可以根据捕获的属性名称和索引访问值。
var data = {
items: ["item", "HI", "item2"],
moreitems: ["moreitem", "moreitem1", "HELLO"]
}
var str = "hey #items1 hey #moreitems2";
const result = str.replace(
/#(\D+)(\d+)/g,
(_match, attr, index) => data[attr][index]
);
console.log(result);
我正在尝试用匹配的键和索引位置替换字符串中的单词而不删除其他单词。
我想要的输出是:嘿嘿嘿你好
到目前为止我已经尝试过...
var data = {
items: ["item", "HI", "item2"],
moreitems: ["moreitem", "moreitem1", "HELLO"]
}
var str = "hey #items1 hey #moreitems2";
var newStr = '';
var match = str.match(/#(.*?)\d+/g); //get str that starts with # and ends with digits
for (var i = 0; i < match.length; i++) {
var keys = match[i].replace(/#|\d+/g, ''), // remove hash and numbers to match the data keys
pos = match[i].replace(/\D/g, ''); // get the digits in str
newStr += data[keys][pos] + ' ';
}
console.log(newStr)
感谢您的帮助!
你可以使用
- string.replace替换进程末尾的substr
- 通过
match[match.length - 1]
恢复匹配字符串的最后一个字符来计算字符串数组中的索引
- 通过删除第一个和最后一个字符恢复要使用的数据类别
category = match.slice(0, -1).substring(1);
var data = {
items: ["item", "HI", "item2"],
moreitems: ["moreitem", "moreitem1", "HELLO"]
}
var str = "hey #items1 hey #moreitems2";
var newStr = str;
var match = str.match(/#(.*?)\d+/g);
var index, category;
console.log(match);
match.forEach(match => {
index = match[match.length - 1];
category = match.slice(0, -1).substring(1);
newStr = newStr.replace(match, data[category][index]);
});
console.log(newStr)
const dic = {
xxx: 'hello',
yyy: 'world',
zzz: 'peace'
}
const string = 'hey! #xxx, #yyy! #zzz on you'
const newString = string.replace(/#\w+/g, item => dic[item.substring(1)])
console.log(string)
console.log(newString)
一个简单的解决方案是将 replace()
与替换函数一起使用。
我使用正则表达式 /#(\D+)(\d+)/g
。匹配 #
,后跟一个或多个 non-digits(放置在捕获组 1 中),后跟一个或多个数字(放置在捕获组 2 中)。
所有捕获组都作为替换函数的参数传递。完整匹配作为第一个参数传递,捕获组 1 作为第二个参数传递,依此类推。
在替换函数中,您可以根据捕获的属性名称和索引访问值。
var data = {
items: ["item", "HI", "item2"],
moreitems: ["moreitem", "moreitem1", "HELLO"]
}
var str = "hey #items1 hey #moreitems2";
const result = str.replace(
/#(\D+)(\d+)/g,
(_match, attr, index) => data[attr][index]
);
console.log(result);