What is the Regexp equivalent of split("") in JS?

Asked

Viewed 44 times

-6

I want to split the number PI 3.141593 into an array that does not contain '.' and all separate numbers. I tried to split . split(/[ .""]/g) but it didn’t work. What would be the equivalent solutions avoiding brute force?

  • 1

    On a line without regex (which is probably not the right tool for this type of operation): [...Math.PI.toString().replace('.', '')];.

1 answer

-2


Forehead there, here it worked:

const pi = 3.141593;

const piArray = pi.toString().replace(".", "").split("");

console.log(piArray);

If you need to mount a string and not an array, apply toString() to the piArray variable afterwards.

const pi = 3.141593;

const piArray = pi.toString().replace(".", "").split("");

console.log(piArray.toString());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.