I have a string

'${user name} ${action} ${what} some_text' 

Is there any normal way to split this string into an array using split?

Expected:

 ['${user name}', '${action}', '${what}', 'some_text'] 

A solution without regulars is highly desirable, if that is possible.

    1 answer 1

    Using regular expression

     var str = '${user name} ${action} ${what} some_text'; var re = /(\$\{.*?\}|\w+)/g; var found = str.match(re); console.log(found); 

    Result

      ["${user name}", "${action}", "${what}", "some_text"] 

    Example http://jsfiddle.net/q5hugvtx/

    • Such a question, and if the line is with an exclamation mark, which will stand in the butt, and it should also be placed in a separate cell of the array for example: "Hello ${user name}!" wait ['Hello', '${user name}', "!"] - pan4o_dp
    • @ pan4o_dp, jsfiddle.net/q5hugvtx/2 - Visman