如何将组合字符串拆分为数组中的多个字符串?

How to split combined string into multiple strings in array?

如何用逗号分隔这个数组? 我有多个数组

[“LeadershipPress”]
[“BusinessLeaderLeadershipPress”]
[“LeaderLeadershipPoliticsPress”]
等等

scraper.scrapeListingPages('article.article', (item) => { 
    var categories = $(item).find('a[rel = "category tag"]').text().split();
    console.log(categories);
    categories.forEach(function(i){
       $(i).find('a[rel = "category tag"]')
       console.log(i);
    })
});

现在我在控制台的输出是

Array [ "BusinessLeaderLeadershipPress" ]
BusinessLeaderLeadershipPress

我想用逗号将类别分成一个数组,而不必使用分隔符、限制或正则表达式,因为我有多个随机数组。

有没有一种方法可以使用 forEach 或 for 循环来完成此操作?

我要的结果是["Business, Leader, Leadership, Press"]

谢谢

您可以拆分字符串并提前查找大写字母。

const
    string = 'BusinessLeaderLeadershipPress',
    result = string.split(/(?=[A-Z])/);

console.log(result);

without having to use separator, limits or regex because I have multiple random arrays.

使用 for-loop 算法 的简单方法考虑到字符串以 大写 : 循环遍历字符串的字符 如果 char 不是 UPPERCASE 将字符累积到变量 word,直到遇到 大写字母,然后压入res数组。

const string = "BusinessLeaderLeadershipPress";
let i = 1;
let character = "";
let word = string[0];
const res = [];
while (i <= string.length) {
  character = string.charAt(i);
  if (character == character.toUpperCase()) {
    res.push(word);
    word = character;
  } else {
    word += character;
  }
  i++;
}
console.log(res); //['Business', 'Leader', 'Leadership', 'Press']