Abk - 24 days ago 14

Javascript Question

I have the following variables

`var arithmetic = "96-66.33+99.3/22*58.2";`

var arr;

var operator = "/*+-^";

I want to

`arithmetic`

`index`

`operator`

`index`

`operator`

The final result will be

`["96-66.33", "+99.3", "/22", "*58.2"];`

`var arithmetic = "96-66.33+99.3/22*58.2";`

arithmetic = arithmetic.split(/(?=[-+*\/])/);

console.log(arithmetic);

The

`arithmetic`

The

`arithmetic`

If I can still get result to be

`["96", "-66.33", "+99.3", "/22", "*58.2"];`

Answer

Closest I could get you through regexp. Very close but not it exactly yet.

```
var arithmetic = "96-66.33+99.3/22*58.2";
arithmetic = arithmetic.split(/([-+*\/])/g);
console.log(arithmetic);
```

EDIT: Got it.

```
var arithmetic = "96-66.33+99.3/22*58.2";
arithmetic = arithmetic.split(/(?=[-+*\/])/);
console.log(arithmetic);
```

```
var arithmetic = "96-66.33+99.3/22*58.2";
arithmetic = arithmetic.split(/(?=[-+*\/])/);
console.log(arithmetic);
```

Source (Stackoverflow)

Comments