【问题标题】:Javascript Regex - How can I delete words with an uppercase letter in the middle?Javascript Regex - 如何删除中间有大写字母的单词?
【发布时间】:2021-04-28 05:59:12
【问题描述】:

我有一个包含一个句子的原始字符串,我正在尝试删除其中 中间有一个大写字母的每个单词。

示例: "I like strawBerries" 将返回 "I like"

但是"I like Strawberries" 会返回"I like Strawberries"

我找到了以下正则表达式:\B[A-Z]\B 但它只匹配单词内部的大写,而不是单词本身......

【问题讨论】:

  • 使用字母,\b[a-z]+[A-Z][a-zA-Z]+\b\b[a-z]+[A-Z][a-zA-Z]*\b 如果大写字母可以出现在单词的末尾。

标签: javascript regex


【解决方案1】:

试试表达式\w+[A-Z]\w*,它匹配除第一个以外的任何位置至少包含一个大写字母的任何单词。

它将匹配所有:

  • xyZ
  • xYz
  • xYZ
  • XyZ
  • XYz
  • XYZ

但不是:

  • xyz
  • xyz

let string = 'This is NOT a RainBow, it is, in faCt, a sTrABerry cakE.';
//  Removed:          xxx   xxxxxxx            xxxx    xxxxxxxxx xxxx
// 'This' is kept.

let out = string.replace(/\w+[A-Z]\w*/g, '');

console.log(out);

【讨论】:

  • 效果也很好,但我可能不够具体,唯一应该删除这个词的场景是“xYz”——Wiktor Stribiżew 提供的答案对我来说是完美的。非常感谢!
【解决方案2】:

你可以使用非 caturing 组来做到这一点:

const regex = /(?:\w+[A-Z]\w+)|(?:\w+[A-Z])|(?:[A-Z]\w+)/gm;
const str = `I like strawBerries. my name is Taimoor `;
const subst = ``;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);
// Substitution result: I like . my name is  

\w 匹配任何单词字符(相当于 [a-zA-Z0-9_])
+ 匹配前一个标记一次到无限次,尽可能多次,根据需要返回 (贪心)
[A-Z] - A-Z 匹配 A(索引 65)和 Z(索引 90)之间的单个字符(区分大小写)
(?:...) 是非捕获组

【讨论】:

    【解决方案3】:

    使用

    /\s*\b[a-z]+[A-Z][a-zA-Z]+\b/g
    

    regex proof

    解释

    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      [a-z]+                   any character of: 'a' to 'z' (1 or more
                               times (matching the most amount possible))
    --------------------------------------------------------------------------------
      [A-Z]                    any character of: 'A' to 'Z'
    --------------------------------------------------------------------------------
      [a-zA-Z]+                any character of: 'a' to 'z', 'A' to 'Z'
                               (1 or more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    

    JavaScript sn-p

    ["I like strawBerries", "I like Strawberries"].forEach(x =>
        console.log(x.replace(/\s*\b[a-z]+[A-Z][a-zA-Z]+\b/g, '').trim())
    )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2019-03-04
      • 1970-01-01
      • 2015-07-18
      • 2014-11-23
      • 2022-11-23
      • 2016-01-10
      相关资源
      最近更新 更多