是否可以使用正则表达式在JavaScript中分割任意数量的字符?例子:“这些是____下划线”将返回 ["These are ", " underscores"]这是我到目前为止的内容:"These are ____ underscores".split("_").filter(x => x);但是,我不确定是否有更有效/更好的方法来做到这一点。谢谢。
2 回答

翻过高山走不出你
TA贡献1875条经验 获得超3个赞
let x = 'These are ____ underscores';
let y = x.split(/_+/);
console.log(y);

慕桂英4014372
TA贡献1871条经验 获得超13个赞
您可以按字符拆分,然后过滤非空数组:
var str = "These are ____ underscores"
str.split("_").filter(x => x.length)
添加回答
举报
0/150
提交
取消