正在尝试检索所有以空格、连字符、撇号或字符串开头的大写前导字符

Trying to retrieve all leading Capitalized characters preceded with white-spaces, hyphens, apostrophes, or start of the string

本文关键字:开头 字符串 字符 连字符 检索 空格      更新时间:2023-09-26

尝试检索所有以空格、连字符、撇号或字符串开头的大写前导字符。

样本:

var text = "This is a sample about about JJohnny Apple-Seed and old Mc'Donald.";
text.match(/[A-Z]/g)  // returns  -->  ["T", "J", "J", "A", "S", "M", "D"]

所需输出:["T", "J", "A", "S", "M", "D"]

更新的最终解决方案:

var str = "This is a story about JJohnny Apple-Seed and Old Mc'Donald.";
var myRe = /'b([A-Z])/g;
var myArray;
while ((myArray = myRe.exec(str)) !== null)
{
  var msg = "Found " + myArray[0] + ".  ";
  msg += "Next match starts at " + myRe.lastIndex-1;
  console.log(msg);
}

标准的'b(单词边界)元字符应该能让你达到目的:

text.match(/'b([A-Z])/g)

给出:

["T", "J", "A", "S", "M", "D"]