Javascript: split function

 let str = 'My-name-is-Lord-Voldemort';
let arr = str.split('-');

Here i’ll get the array

arr[0] = 'My' ; arr[1] = 'name'

What should i do to get this

arr[0] = 'My'; arr[1] = '-';
arr[2] = 'name; arr[3] = '-';

Are you familiar with RegEx? The only way I can think of doing this without using loops and such would be to split it with a Regular Expression. One you use RegEx, this can be done easily. Look into RegEx anchor metacharacters and see if you can find the one that will give you the result you need.

1 Like

I want this result:

let res = '3 * 3' ;
// res is now equal to 9

I know about eval function which will do the job. But i want to do it using a custom function.

How do i convert'*'into * operator

You can’t convert a string to a math operator, but you can convert a string to a function that multiplies numbers…

ps. are you sure you want to do this? When the code starts getting weird, it is best to step back and ask if there is a better way forward.

This is the project i’m working on: