admin管理员组

文章数量:1295783

I have this piece of JavaScript code

price = price.replace(/(.*)\./, x => x.replace(/\./g,'') + '.')

This works fine in Firefox and Chrome, however IE gives me an syntax error pointing at => in my code.

Is there a way to use ES6 arrow syntax in IE?

I have this piece of JavaScript code

price = price.replace(/(.*)\./, x => x.replace(/\./g,'') + '.')

This works fine in Firefox and Chrome, however IE gives me an syntax error pointing at => in my code.

Is there a way to use ES6 arrow syntax in IE?

Share Improve this question edited Oct 18, 2016 at 13:57 user229044 239k41 gold badges344 silver badges346 bronze badges asked Oct 18, 2016 at 13:46 Michael Tot KorsgaardMichael Tot Korsgaard 4,01411 gold badges56 silver badges93 bronze badges 2
  • 2 Until IE wants to become happy, use anonymous method inside replace. – Wiktor Stribiżew Commented Oct 18, 2016 at 13:47
  • 3 Use a transpiler or write ES5 code in the first place. – Bergi Commented Oct 18, 2016 at 13:48
Add a comment  | 

2 Answers 2

Reset to default 21

IE doesn't support ES6, so you'll have to stick with the original way of writing functions like these.

price = price.replace(/(.*)\./, function (x) {
  return x.replace(/\./g, '') + '.';
});

Also, related: When will ES6 be available in IE?

Internet explorer doesn't support arrow functions yet. You can check the browsers supporting arrow functions here.

The method to solve it would be to make a good old regular callback function :

price = price.replace(/(.*)\./, function (x) {
    x.replace(/\./g,'') + '.';
}

This would work in every browser.

本文标签: javascriptSyntax error in IE using ES6 arrow functionsStack Overflow