0

I'm not very strong with RegEx but I am using a small regex expression and need assistance. The expression works, but I have been asked to turn the regex expression into a constant variable.

What's the best way for me to do this? Here is the code in question.

  const storeNameDetails = (store) => {
    const truncStoreNo = store.storeNo?.padStart(4, '0');
    return STORE_LIST[truncStoreNo]?.replace(/ *\([^)]*\) */g, '');
  };
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • 1
    Not sure why someone closed this question and linked this useless answer... You create a new regex object with every call to your function. That includes some initialization overhead that can be eliminated by storing the regex outside of your function `const re = / *\([^)]*\) */g` and then inside re-use it `myString.replace(re, '')` see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – Christoph Lütjen Jun 08 '20 at 15:08
  • Christoph, thank you. I was a bit confused by the close and link as well as it wasn't related. The issue I was having was the ' ' in the .replace, I was trying to include it in the regex constant. Thank you for your help, this solves my problem! – KleinBottleCode Jun 08 '20 at 15:23

0 Answers0