简体   繁体   English

正则表达式用逗号分隔3个字母的单词

[英]Regex for comma separated 3 letter words

I want to create a regex for exactly 3 letter words only, separated by commas. 我只想为正好3个字母的单词创建一个正则表达式,并用逗号分隔。 3 letter words can be padded with space(at most 1 on each side) 3个字母词可以用空格填充(每边最多1个)

Valid Examples: 有效示例:

ORD
JFK, LAX
ABC,DEF, GHK,REW, ASD

Invalid Examples: 无效的示例:

ORDA
OR
ORD,
JFK, LA

I tried the following but couldn't get it to work. 我尝试了以下操作,但无法正常工作。

^(?:[A-Z ]+,)*[A-Z ]{3} +$ 

Try this pattern: 试试这个模式:

^[A-Z]{3}(?:[ ]?,[ ]?[A-Z]{3})*$

This pattern matches an initial three letter word, followed by two more terms separated by a comma with optional spaces. 此模式与第一个三个字母的单词匹配,后跟两个以上的单词,并用逗号分隔,并带有可选的空格。

Try this: ^([ ]?[AZ]{3}[ ]?,)*([ ]?[AZ]{3}[ ]?)+$ 试试这个: ^([ ]?[AZ]{3}[ ]?,)*([ ]?[AZ]{3}[ ]?)+$

https://regex101.com/r/HFeN0D/2/ https://regex101.com/r/HFeN0D/2/

It matches at least one three letter word (with spaces), preceded by any number of words three letter words with commas after them. 它匹配至少一个三个字母的单词(带空格),后跟任意数量的单词,三个字母后面带有逗号。

You can do this with the pattern: ^((:? ?[AZ]{3} ?,)*(?: ?[AZ]{3} ?))+$ 您可以使用以下模式执行此操作: ^((:? ?[AZ]{3} ?,)*(?: ?[AZ]{3} ?))+$

 var str = `ORD JFK, LAX ABC,DEF, GHK,REW, ASD ORDA OR ORD, JFK, LA`; let result = str.match(/^((:? ?[AZ]{3} ?,)*(?: ?[AZ]{3} ?))+$/gm); document.getElementById('match').innerHTML = result.join('<br>'); 
 <p id="match"></p> 

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

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