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% 

    2 answers 2

    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); 

    • thanks, just what you need - user313812
    • @GooxFoxy, not at all :) - Let's say Pie

    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]))); 

    • Sorry, but the lines are not in the array - user313812
    • Thanks for your option, it is also quite good - user313812
    • @GooxFoxy, in my decision does not matter, one line or more in an array - Hekumok