0

I want to allow my variable to be a part of regex. For instance:

const delimiter = '.';

I want to to use it like this:

someString
  .replace(/[^0-9{delimiter}-]/g, '')
  .replace(/(\.{delimiter}*)\./g, '$1')
  .replace(/(?!^)-/g, '');

instead of

someString
  .replace(/[^0-9.-]/g, '')
  .replace(/(\..*)\./g, '$1')
  .replace(/(?!^)-/g, '');

How to achieve this? Actually, the reason why it's needed it to remove all non-numeric and non-decimal symbols from the given string. The issue is the format delimiter in different locales, so the delimiter can be . or , depending on the localization.

UPD: I saw that my question is duplicated, but in the original answer the example is pretty simple and gives me nothing. Especially, I don't understand how to split /[^0-9.-]/g to /[^0-9 and -]/g to put my variable in between.

max
  • 612
  • 7
  • 26

0 Answers0