假设我有一个字符串 str = "a b c d e"
。 str.split(' ')
给我一个元素数组 [a,b,c,d,e]。
我如何使用正则表达式来获得此匹配?
最佳答案
String.split() 支持正则表达式作为参数;
String.prototype.split([separator[, limit]])
let str = 'a b c d e';
str.split(/ /);
// [ 'a', 'b', 'c', 'd', 'e' ]
let str = 'a01b02c03d04e';
str.split(/\d+/);
// [ 'a', 'b', 'c', 'd', 'e' ]
关于node.js - 使用正则表达式按空格分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44772825/