找一个有大写字母的单数词

时间:2016-12-20 14:24:26

标签: javascript regex node.js

所以我正在使用javascript服务器,它为我解析一些文本。我正在寻找一个带有大写字母的单数词。我知道这个问题可以通过正则表达式解决,虽然我不知道如何做到这一点,并且正在寻找一些指导和一些事情。

例如:

"我昨天去看了我的朋友约翰"

将返回" John"因为大写字母,但不会返回" I",因为它是句子中的第一个单词。

非常感谢

4 个答案:

答案 0 :(得分:1)

简单的答案:

In [656]: # Inputs 
     ...: a = np.array(['a', 'b', 'a', 'a', 'b', 'a'])
     ...: b = np.array([150, 154, 147, 126, 148, 125])
     ...: 

In [657]: mask = a!="b"

In [658]: mask
Out[658]: array([ True, False,  True,  True, False,  True], dtype=bool)

# Crux of the implmentation happens here :
In [696]: np.where(mask,np.arange(mask.size),0)
Out[696]: array([0, 0, 2, 3, 0, 5])

In [697]: np.maximum.accumulate(np.where(mask,np.arange(mask.size),0))
Out[697]: array([0, 0, 2, 3, 3, 5])# Stepped indices "intervaled" at masked places

In [698]: idx = np.maximum.accumulate(np.where(mask,np.arange(mask.size),0))

In [699]: b[idx]
Out[699]: array([150, 150, 147, 126, 126, 125])

它匹配任何不在行开头的大写单词(使用否定前瞻和单词边界检查)。

See it here at regex101

但你的要求很粗略......

  

约翰和我是好朋友。

会返回(?!^)\b[A-Z]\w* 。这真的是你想要的吗?

它只适用于连续的第一句话。以非字符开头的行也将失败。 E.g

  

- 我喜欢约翰,她说。

     

"这是一个引用。"

Check out this extended regex101

答案 1 :(得分:0)

"I went to to go see my friend John yesterday".replace(/\s[A-Z][a-z]+/g, ' ***')

答案 2 :(得分:0)

大写字母后跟至少一个非白色字符:

[A-Z]\S+

答案 3 :(得分:0)

可能是这样的,抱歉,如果这不是你想要的那样

var text = "I went to to go see my friend John yesterday";
var arr = text.split(' ');

var upperCaseText = [];

for(var i = 1; i < arr.length; i++) {
  if(/[A-Z]/.test(arr[i])) { 
    upperCaseText.push(arr[i]);
  }
} 

console.log(upperCaseText);
相关问题