我试图拆分字符串包括字符串,但空间也吐了.
我的代码: -
var a =' that i love
game1 ';
console.log(a.split(' '))
Run Code Online (Sandbox Code Playgroud)
我目前的输出如下: -
(57) ["?", "", "", "", "", "", "", "", "", "that", "i", "love?", "", "", "", "", "", "", "", "", "game1???", "", "", "", ""]
Run Code Online (Sandbox Code Playgroud)
输出我试图得到这样的东西: -
(4)[" that",'i',' love',' ?game'];
Run Code Online (Sandbox Code Playgroud)
如何以这种方式分割字符串,包括空格和换行?
您可以将String#match与正则表达式(regex101)一起使用,以获得类似于您想要的内容:
var a =` that i love
game1 `;
console.log(a.match(/\s*\S*/g));
// or
console.log(a.match(/\s*\S*\s*/g));Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |