Question: I have a string value to which i want to split and preserve all the characters upto last space.

```
e.g var x="item1 item2 item3 *stopMarker" 
var y=someRegOpr(x); and y will contain only "item1 item2 item3"

```

How to do this by a Regular Expression.

Thanks

Answer: 
What if you use [`lastIndexOf()`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/lastIndexOf) instead:

```
var x = "item1 item2 item3 *stopMarker";
x.substring(0, x.lastIndexOf(" "));  // "item1 item2 item3"

```

---

**UPDATE:** Hack for a single word without spaces:

```
var x = "item1";
x.substring(0, Math.max(0, x.lastIndexOf(" ")) || Infinity);  // "item1"

```

---

**UPDATE2:** All the same using regex:

```
"item1 item2 item3 *stopMarker".split(/ [^ ]*$/).pop();  // "item1 item2 item3"

```
Use "followed by" expression:

```
"item1 item2 item3 *stopMarker".match( /^.*(?=\s)/i )
>> ["item1 item2 item3"]

```