# JavaScript split at a maths operator while still including it

JavaScript split at a maths operator while still including it

Okay… I’m trying to split a string of text up into an array where instances of ‘+’ or ‘-‘ appear. First of all I need to know how to split at the plus sign and include it.

I have tried this: `'3+5'.split(/(?='+')/)`

and the result I want is `["3","+5"]`

but it doesn’t seem to work and only produces and array of `["3+5"]`

which is cool but not what I’m looking for. All the resources I have found say that this should work but it doesn’t how I want it. I tried it with commas in place of the plus sign and it yields the result I was looking for but I kind of need it for the plus sign.

Secondly I would like to split it at a minus in the same way as well. The plus not working I don’t really have code for it but I assume it goes a little like this from the resources I found: `'3+5-2'.split(/(?=+) | (?=-)/)`

with the result of `["3","+5","-2"]`

## solution

It looks like you want `/(?=[+])/`

or `/(?=\+)/`

rather than `/(?='+')/`

.

The `+`

needed to be escaped.

```
'3+5'.split(/(?=[+])/);
// ["3", "+5"]
```

or:

```
'3+5'.split(/(?=\+)/);
// ["3", "+5"]
```

If you want to include `-`

as a delimiter:

```
'3+5-2'.split(/(?=[+-])/);
// ["3", "+5", "-2"]
```