【问题标题】:Regex not Capturing Properly?正则表达式没有正确捕获?
【发布时间】:2019-11-04 23:24:31
【问题描述】:

所以我有代码尝试将字符串拆分为小写字母。我以为我做对了,但是当我运行代码时,数组有 8 个包含“”的单元格,也就是什么也没有。 例如,如果字符串是:“我有糖果”,我希望数组返回:[“h”、“a”、“v”、“e”、“s”、“w”、“e”、“ e", "t", "s"]

这是我的代码:

let s1 = "Are they here";
let words1 = s1.split(/([a-z])/g);

【问题讨论】:

  • split 将在每次出现时拆分模式,在这种情况下为小写字母。

标签: javascript arrays regex


【解决方案1】:

只匹配每个小写字符会更容易:

let s1 = "Are they here";
let words1 = s1.match(/[a-z]/g);
console.log(words1);

【讨论】:

  • 哦,这行得通!我什至不知道比赛,我很感激!
  • @sonyansachdev 不用担心 - 我很高兴能提供帮助。
猜你喜欢
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多