There are lines of this type
H 40% K 45%, S 15% K 15%, RUB 23%, SH 20% And array
var array = {'H': 'Hlopok','K': 'Karton','RUB': 'Rubin','SH': 'Shelk'}; How to make such a result
Hlopok 40% Karton 45%, S 15% Karton 15%, Rubin 23%, Shelk 20% There are lines of this type
H 40% K 45%, S 15% K 15%, RUB 23%, SH 20% And array
var array = {'H': 'Hlopok','K': 'Karton','RUB': 'Rubin','SH': 'Shelk'}; How to make such a result
Hlopok 40% Karton 45%, S 15% Karton 15%, Rubin 23%, Shelk 20% As an option:
var str = 'H 40% K 45%, S 15% K 15%, RUB 23%, SH 20%'; var array = {'H': 'Hlopok','K': 'Karton','RUB': 'Rubin','SH': 'Shelk'}; result = str.replace(/\w+(?=\s\d+%)/g, m => array[m] ? array[m] : m); console.log(result); As an option
const stringList = [ 'H 40%', 'K 45%, S 15%', 'K 15%, RUB 23%, SH 20%', ]; const match = {'H': 'Hlopok','K': 'Karton','RUB': 'Rubin','SH': 'Shelk'}; const regex = new RegExp('\\b(' + Object.keys(match).join('|') + ')\\b', 'g'); console.log(stringList.map((str) => str.replace(regex, (matched) => match[matched]))); Source: https://ru.stackoverflow.com/questions/898723/
All Articles