Skip to content Skip to sidebar Skip to footer

Javascript: Split A String Into Array Matching Parameters

I have a string that has numbers and math operators (+,x,-, /) mixed in it '12+345x6/789' I need to convert it into an array seperated by those math operators. [12, +, 345, x, 6, /

Solution 1:

Splitting on consecutive non-digit chars \D+ you get

console.log ('12+345x6/789'.split (/\D+/))
// [ '12', '345', '6', '789' ]

If you add a capture group, (\D+) you get the separator too

console.log ('12+345x6/789'.split (/(\D+)/))
// [ "12", "+", "345", "x", "6", "/", "789" ]

If you want to support parsing decimals, change the regexp to /([^0-9.]+)/ - Note, \D used above is equivalent to [^0-9], so all we're doing here is adding . to the character class

console.log ('12+3.4x5'.split (/([^0-9.]+)/))
// [ "12", "+", "3.4", "x", "5" ]

And a possible way to write the rest of your program

constcont = x =>
  k => k (x)

constinfix = f => x =>
  cont (f (x))

constapply = x => f =>
  cont (f (x))

constidentity = x =>
  x

const empty =
  Symbol ()
  
constevaluate = ([ token = empty, ...rest], then = cont (identity)) => {
  if (token === empty) {
    return then
  }
  else {
    switch (token) {
      case"+":
        return evaluate (rest, then (infix (x =>y => x + y)))
      case"x":
        return evaluate (rest, then (infix (x =>y => x * y)))
      case"/":
        return evaluate (rest, then (infix (x =>y => x / y >> 0)))
      default:
        return evaluate (rest, then (apply (Number (token))))
    }
  }
}

constparse = program =>
  program.split (/(\D+)/)
  
constexec = program =>
  evaluate (parse (program)) (console.log)

exec ('')             // 0
exec ('1')            // 1
exec ('1+2')          // 3
exec ('1+2+3')        // 6
exec ('1+2+3x4')      // 24
exec ('1+2+3x4/2')    // 12
exec ('12+345x6/789') // 2

Solution 2:

If you are unconcerned with whitespace, all you need is

'12+345x6/78-9'.match(/\d+|[\+-\/x]/g);

which splits the string into numbers and the +, -, \, and x tokens.

'use strict';
const tokens = '12+345x6/78-9'.match(/\d+|[\+-\/x]/g);

console.log(tokens);

To handle whitespace, consider

'12+3 45 x6/78-9'.match(/\d|\d[\s\d]\d|[\+-\/x]/g);

which splits the string into numbers (optionally allowing whitespace as a digit separator within a single number) and +, -, \, and x.

'use strict';
const tokens = '12+3 45 x6/78-9'.match(/\d+\s?\d+|\d+|[\+-\/x]/g);


console.log(tokens);

Solution 3:

this is going to work

console.log('12+345x6/789'.match(/\D+|\d+/g))

Post a Comment for "Javascript: Split A String Into Array Matching Parameters"