简体   繁体   English

如何通过正则表达式对以下字符串进行分组

[英]How to group the following strings by regular expression

This is the string I want to process.(At least one of the underlined parts. The last part is never underlined ) 这是我要处理的字符串。(至少有一个带下划线的部分。最后一部分从未加下划线)

'_A._B._C._D._F.f'`

I expected 我期望

["A", "B", "C", "D", "F", "f"]

How to achieve the same effect by regularity, I tried, but can't loop the same format part. 如何通过规律性地实现同样的效果,我试过,但不能循环相同的格式部分。

new RegExp('^[(_(.+)\\.)]+(.+)$')

You could exclude dot and underscore from matching. 您可以从匹配中排除点和下划线。

 var string = '_A._B._C._D._F.f', result = string.match(/[^._]+/g); console.log(result); 

How about that without using regex? 不使用正则表达式怎么样?

 str = '_A._B._C._D._F.f'.split('.') var alphabets = str.map(c => c.replace('_', '')); console.log(alphabets); 

You can use split that removes [._]+ (any substring containing dots or floors) and the filter (to remove the initial empty string): 您可以使用split删除[._]+ (包含点或楼层的任何子字符串)和filter (删除初始空字符串):

'_A._B._C._D._F.f'.split(/[._]+/).filter(function(s){ return s.length > 0})
# => [ "A", "B", "C", "D", "F", "f" ]

EDIT: Simplification suggested in comments: 编辑:评论中建议的简化:

'_A._B._C._D._F.f'.split(/[._]+/).filter(Boolean)
# =>  [ "A", "B", "C", "D", "F", "f" ]

In your regex you try to match the whole pattern using an anchor ^ to assert the start of the string followed by a character class which will match only one out of several characters (and might for example also be written as [_(+\\\\.)]+ ) and then you capture the rest of the string in a capturing group and assert the end of the line $ . 在你的正则表达式中,你尝试使用一个锚来匹配整个模式^来断言字符串的开头,然后是一个字符类 ,它只匹配几个字符中的一个(例如也可以写成[_(+\\\\.)]+ )然后你捕获捕获组中的其余字符串并断言$行的结尾。

If you want to check the format of the string first, you might use a more exact pattern. 如果要先检查字符串的格式,可以使用更精确的模式。 When that pattern matches, you could do a case insensitive match for a single character as the pattern is already validated: 当该模式匹配时,您可以对单个字符执行不区分大小写的匹配,因为该模式已经过验证:

 const regex = /^_[AZ](?:\\._[AZ])+\\.[az]$/; const str = `_A._B._C._D._F.f`; if (regex.test(str)) { console.log(str.match(/[az]/ig)); } 

See the regex demo 请参阅正则表达式演示

That will match: 这将匹配:

  • ^ Assert the start of the strin ^断言strin的开始
  • _[AZ] Match an underscore and an uppercase character _[AZ]匹配下划线和大写字符
  • (?:\\._[AZ])+ 1+ times repeated grouping structure to match ._ followed by an uppercase character (?:\\._[AZ])+ 1+次重复分组结构以匹配._后跟一个大写字符
  • \\.[az] Match a dot and a lowercase character \\.[az]匹配点和小写字符
  • $ Assert the end of the line $断言该行的结尾

字符串方法.match与全局标志,可以帮助您:

 console.log('_A._B._C._D._F.f'.match(/[az]+/gi)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM