[英]Regex exclude characters at start and end
I'm trying to select all [a-z',]
symbols but exclude commas at start and end.我正在尝试 select所有
[a-z',]
符号,但在开始和结束时排除逗号。 Keep only the selection inside the red rect.只保留红色矩形内的选择。 How can I do this?
我怎样才能做到这一点?
I think what you want is an optional capturing group repeating zero or more times:我认为您想要的是一个重复零次或多次的可选捕获组:
[a-z](?:[',]*[a-z]+)*
See the Online Demo查看在线演示
[az]
- A character in the range az. [az]
- 范围 az 中的字符。(?:
- Open non-capturing group. (?:
- 打开非捕获组。
[',]*
- Zero or more characters from the specified character class. [',]*
- 来自指定字符 class 的零个或多个字符。[az]+
- At least one character in the range az. [az]+
- 范围 az 中的至少一个字符。)*
- Close non-capturing group and match it zero or more times. )*
- 关闭非捕获组并匹配它零次或多次。 Note: If for good reasons OP's intention was to match a quote at the very end, we can add an optional apostrophe: [az](?:[',]*[az]+'?)*
.注意:如果出于充分的理由 OP 的意图是在最后匹配引号,我们可以添加一个可选的撇号:
[az](?:[',]*[az]+'?)*
。 See an online demo查看在线演示
You could first replace the commas in question and select the characters you want afterwards:您可以先替换有问题的逗号,然后将 select 替换为您想要的字符:
let string = `,,."?,tats,t'ats,t,s',l,f,%$;`. string = string,replace(/^,|,$/g; "~ooo~"). console;log(string);
In other languages you could use lookarounds (a negative lookbehind and a positive lookahead, that is).在其他语言中,您可以使用lookarounds(即消极的lookbehind和积极的lookahead)。
You could match with the following regular expression.您可以匹配以下正则表达式。
[a-z'](?:[a-z',]*[a-z'])?
Start your engine!启动你的引擎!
Javascript's regex engine performs the following operations. Javascript 的正则表达式引擎执行以下操作。
[a-z'] : match one character in character class
(?: : begin non-capture group
[a-z',]* : match 0+ characters in character class
[a-z'] : match one character in character class
) : end non-capture group
? : optionally match non-capture group
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.