[英]Regex match all punctuations except 'D.C.'
我正在尝试编写一个查找所有标点符号 [.!?] 的正则表达式,以便将下一个单词大写,但是如果句点是字符串 'DC' 的一部分,它应该被忽略,到目前为止我有第一部分工作,但不确定如何忽略“DC”
const punctuationCaps = /(^|[.!?]\\s+)([az])/g;
You can match the DC
part and use an alternation using the 2 capturing groups that you already have.您可以匹配
DC
部分并使用您已有的 2 个捕获组进行交替。
In the replacement check for one of the groups.在其中一组的替换检查中。 If it is present, concatenate them making group 2 toUpperCase(), else return the match keeping DC in the string.
如果存在,则将它们连接起来形成第 2 组 toUpperCase(),否则返回将 DC 保留在字符串中的匹配项。
const regex = /D\\.C\\.|(^|[.!?]\\s+)([az])/g; let s = "this it DC test. and? another test! it is."; s = s.replace(regex, (m, g1, g2) => g2 ? g1 + g2.toUpperCase() : m); console.log(s);
Use a negative lookahead:使用负前瞻:
var str = 'is DC a capital? i don\\'t know about XY stuff.'; var result = str.replace(/(^|[.!?](?<![AZ]\\.[AZ]\\.)\\s+)([az])/g, (m, c1, c2) => { return c1 + c2.toUpperCase(); }); console.log('in: '+str); console.log('out: '+result);
Console output:控制台输出:
in: is D.C. a capital? i don't know about X.Y. stuff.
out: Is D.C. a capital? I don't know about X.Y. stuff.
Explanation:解释:
(^|[.!?])
- expect start of string, or a punctuation char (^|[.!?])
- 期待字符串的开始,或标点字符(?<![AZ]\\.[AZ]\\.)
- negative lookahead: but not a sequence of upper char and dot, repeated twice (?<![AZ]\\.[AZ]\\.)
- 负前瞻:但不是高位字符和点的序列,重复两次\\s+
- expect one or more whitespace chars \\s+
- 期待一个或多个空白字符([az])
- expect a lower case char, in parenthesis for second capture group ([az])
- 期望一个小写字符,在第二个捕获组的括号中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.