简体   繁体   English

编写一个与用破折号分隔的字符串匹配但应全部为大写或小写的正则表达式

[英]Write a regex that matches with string separated by dash but should be all upper case or lower case

I am writing a regex that checks for the strings like我正在编写一个正则表达式来检查字符串,例如

ju-NIP-er-us skop-u-LO-rum and ui-LA-ui LO-iu-yu ju-NIP-er-us skop-u-LO-rumui-LA-ui LO-iu-yu

the set of characters separated by the --分隔的字符集

this is what I have got这就是我所拥有的

 let str = "ju-NIP-er-us skop-u-LO-rum"; let str2 = "jU-NiP-Er-us skop-u-LO-rum" console.log(/^\p{L}+(?:[- ']\p{L}+)*$/u.test(str)) // this matches console.log(/^\p{L}+(?:[- ']\p{L}+)*$/u.test(str2)) // this also matches but this shouldn't match

The problem is set of characters separated by the - should either be all capital or all smaller,but right now it is matching the mix characters as well,see the snippet.How to make this regex match only all small or all caps between dashes.问题是由 - 分隔的字符集应该全部为大写或全部较小,但现在它也匹配混合字符,请参阅片段。如何使此正则表达式仅匹配破折号之间的所有小写或全部大写。

You may use this regex to make sure to match same case substrings between - or space or ' delimiters:您可以使用此正则表达式来确保匹配-或空格或'分隔符之间的相同大小写子字符串:

^(?:\p{Lu}+|\p{Ll}+)(?:[- '](?:\p{Lu}+|\p{Ll}+))*$

RegEx Demo正则表达式演示

Non-capturing group (?:\p{Lu}+|\p{Ll}+) matches either 1+ of uppercase unicode letters or 1+ of lowercase unicode letters but not a mix of both cases.非捕获组(?:\p{Lu}+|\p{Ll}+)匹配 1+ 个大写 unicode 字母或 1+ 个小写 unicode 字母,但不是两种情况的混合。

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

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