Skip to main content
added 2 characters in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

ECMAScript 6, 125127 bytes

Here is my regex attempt (using a single regex and some logic in the replacement callback):

i.replace(/(^|[*+-])(\d+|(?:([\d.]+)\^)?(x)(?:\^(-?[\d.]+))?)(?![.*^])/g,(m,s,a,b,x,e)=>s+(b?'ln'+b+'*'+a:e?e--+'*x^'+e:x?1:0))

This expects the input string to be stored in i and simply returns the result. Try it out in an ECMAScript 6 compliant console (like Firefox's).

ECMAScript 6, 125 bytes

Here is my regex attempt (using a single regex and some logic in the replacement callback):

i.replace(/(^|[*+-])(\d+|(?:([\d.]+)\^)?(x)(?:\^([\d.]+))?)(?![.*^])/g,(m,s,a,b,x,e)=>s+(b?'ln'+b+'*'+a:e?e--+'*x^'+e:x?1:0))

This expects the input string to be stored in i and simply returns the result. Try it out in an ECMAScript 6 compliant console (like Firefox's).

ECMAScript 6, 127 bytes

Here is my regex attempt (using a single regex and some logic in the replacement callback):

i.replace(/(^|[*+-])(\d+|(?:([\d.]+)\^)?(x)(?:\^(-?[\d.]+))?)(?![.*^])/g,(m,s,a,b,x,e)=>s+(b?'ln'+b+'*'+a:e?e--+'*x^'+e:x?1:0))

This expects the input string to be stored in i and simply returns the result. Try it out in an ECMAScript 6 compliant console (like Firefox's).

deleted 1 character in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

JavaScriptECMAScript 6, 141125 bytes

Here is my regex attempt (using a single regex and some logic in the replacement callback):

i.replace(/(^|[*+-])(\d+|(?:([\d.]+)\^)?(x)(?:\^([\d.]+))?)(?![.*^])/g,function(m,s,xa,b,x,e){return s+=>s+(x=='x'?1:b?'ln'+b+'*'+x'ln'+b+'*'+a:e?e--+'*x^'+e:x?1:0)})

If only CoffeeScript hadThis expects the ternary operator, this would even come closeinput string to be stored in i and simply returns the Perl solutionresult. Try it out in an ECMAScript 6 compliant console :(like Firefox's).

JavaScript, 141 bytes

Here is my regex attempt (using a single regex and some logic in the replacement callback):

i.replace(/(^|[*+-])(\d+|(?:([\d.]+)\^)?x(?:\^([\d.]+))?)(?![.*^])/g,function(m,s,x,b,e){return s+(x=='x'?1:b?'ln'+b+'*'+x:e?e--+'*x^'+e:0)})

If only CoffeeScript had the ternary operator, this would even come close to the Perl solution. :)

ECMAScript 6, 125 bytes

Here is my regex attempt (using a single regex and some logic in the replacement callback):

i.replace(/(^|[*+-])(\d+|(?:([\d.]+)\^)?(x)(?:\^([\d.]+))?)(?![.*^])/g,(m,s,a,b,x,e)=>s+(b?'ln'+b+'*'+a:e?e--+'*x^'+e:x?1:0))

This expects the input string to be stored in i and simply returns the result. Try it out in an ECMAScript 6 compliant console (like Firefox's).

Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

JavaScript, 141 bytes

Here is my regex attempt (using a single regex and some logic in the replacement callback):

i.replace(/(^|[*+-])(\d+|(?:([\d.]+)\^)?x(?:\^([\d.]+))?)(?![.*^])/g,function(m,s,x,b,e){return s+(x=='x'?1:b?'ln'+b+'*'+x:e?e--+'*x^'+e:0)})

If only CoffeeScript had the ternary operator, this would even come close to the Perl solution. :)