2020年12月30日星期三

javascript: Replace values in a string based on an array, efficient solution

I have a sample code but I am looking for the most efficient solution. Sure I can loop twice through the array and string but I was wondering if I could just do a prefix search character per character and identify elements to be replaced. My code does not really do any of that since my regex is broken.

const dict = {      '\\iota': 'ι',      '\\nu': 'ν',      '\\omega': 'ω',      '\\\'e': 'é',      '^e': 'ᵉ'  }  const value = 'Ko\\iota\\nu\\omega L\'\\\'ecole'    const replaced = value.replace(/\b\w+\b/g, ($m) => {    console.log($m)    const key = dict[$m]    console.log(key)    return (typeof key !== 'undefined') ? key : $m  })  
https://stackoverflow.com/questions/65516491/javascript-replace-values-in-a-string-based-on-an-array-efficient-solution December 31, 2020 at 12:04PM

没有评论:

发表评论