regex match first occurrence of a single character

I’ll make this extremely simple

I have a function that comes across a string as such by design: water 'thing one'

I need to split this string by the first space, since there are spaces elsewhere.

I’ve tried many regex expressions like / .*? /, but they can only match two consecutive spaces.

How do I do this?

Thanks in advance.

1 thought on “regex match first occurrence of a single character”

  1. If you just want the portion of the string before and after the first space, you could use regex replacement here:

    var input = "water 'thing one'";
    var first = input.replace(/[ ].*$/, "");
    var second = input.replace(/^\S*[ ]/, "");
    console.log("first part:  " + first);
    console.log("second part: " + second);
    Reply

Leave a Comment