1

I'm using the following javascript to parse number according to locale. It works fine, but when it fails the YUI Compressor used to compress js. Apparently, the error is at using the backtick ` in the new RegExp constructor. And it is fine if it's removed.

function myparsenumber(value, locale) {
locale = typeof locale !== 'undefined' ? locale : navigator.language;
var example = Intl.NumberFormat(locale).format('1.1');
var cleanPattern = new RegExp(`[^-+0-9${ example.charAt( 1 ) }]`, 'g');
var cleaned = value.replace(cleanPattern, '');
var normalized = cleaned.replace(example.charAt(1), '.');

return parseFloat(normalized);
}

Is there any equivalent constructor or expression to avoid the backtick? Thanks in advance

BARON'S
  • 133
  • 3
  • 15
  • Just use a single quote ' – Poul Bak Nov 22 '18 at 16:34
  • 1
    Forward slashes are more typical. – Andy G Nov 22 '18 at 16:35
  • @Andy G Forward slashes only works with literal constructor, either use single or double quote when using string constructor. – Poul Bak Nov 22 '18 at 16:47
  • Appreciate for replies. Single quote does the job. Though I attempted before using it and failed many times but I tried with '[^-+0-9${ ' + example.charAt( 1 ) + ' }]' and it works now. Thanks again guys. – BARON'S Nov 23 '18 at 03:16

0 Answers0