Javascript 获取字符串并将其放入给定正则表达式的数组中
我有以下结构,要么是单个字符串数组['stufff sjktasjtjser((matchthis))'],要么是嵌套数组中的相同结构['stuff'、['more stuff((matchhere))']、'((Andother))']; 我可以循环并匹配括号中的所有正则表达式,甚至替换文本:Javascript 获取字符串并将其放入给定正则表达式的数组中,javascript,arrays,regex,Javascript,Arrays,Regex,我有以下结构,要么是单个字符串数组['stufff sjktasjtjser((matchthis))'],要么是嵌套数组中的相同结构['stuff'、['more stuff((matchhere))']、'((Andother))']; 我可以循环并匹配括号中的所有正则表达式,甚至替换文本: //after flattening the array lets take the first one, assume I am looping in the first element. var m
//after flattening the array lets take the first one, assume I am looping in the first element.
var matches = currentArrayElement.matchAll('fancyregex') //pretend I am matching the brackets
matchs.forEach(match=>currentArrayElement=currentArrayElement.replaceAll(match[0],'whatwhat'))
console.log(currentArrayElement)//'stufff sjktasjtjser whatwhat'
//but what I actually want is
// currentArrayElement = ['stufff sjktasjtjser','whatwhat'];
有人知道我如何做到这一点吗?或者任何可以在嵌套数组中执行此操作的模板库?我有时需要输出字符串['tss']的数组,有时需要输出带有对象[{}]的数组
谢谢。问题是我需要更改该索引中的数组,而不是整个数组 以下是我当时所做的:
//after flattening the array lets take the first one, assume I am looping in the first element.
var matches = currentArrayElement.matchAll('fancyregex') //pretend I am matching the brackets
matches.forEach((match) => {
currentArrayElement[i] = c.split(match[0]).flatMap(
(value, index, array) => (array.length - 1 !== index
? [value, 'whatwhat',]
: value),
);
});
你能提供一些输入和输出的例子吗?