How to capture an arbitrary number of groups in JavaScript Regexp?

前端 未结 4 1474
鱼传尺愫
鱼传尺愫 2020-11-22 08:38

I would expect this line of JavaScript:

\"foo bar baz\".match(/^(\\s*\\w+)+$/)

to return something like:

[\"foo bar baz\",          


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 09:15

    Unless you have a more complicated requirement for how you're splitting your strings, you can split them, and then return the initial string with them:

    var data = "foo bar baz";
    var pieces = data.split(' ');
    pieces.unshift(data);
    

提交回复
热议问题