简体   繁体   English

正则表达式在开始和结束时排除字符

[英]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.

相关问题 具有多个必须相同的开始和结束字符的正则表达式 - Regex with multiple start and end characters that must be the same 在javascript的正则表达式结果的开始和结尾处插入字符 - Insert characters on start and end of regex result on javascript 以数字之间的字符开头和结尾的正则表达式 - Regex expression for start and end with characters in between numbers 正则表达式从字符串的开头和结尾删除某些字符 - Regex Expression to remove certain characters from string expect at the start and end 使用RegEx排除某些字符 - Exclude certain characters using RegEx 正则表达式开始和结束匹配 - regex start and end matching JavaScript Regex当字母在某些特殊单词的开头/结尾/之间时,如何排除字母替换? - JavaScript Regex How to exclude letter replacement when the letter is start/end/between of some special words? 用于完全阻止特殊字符的正则表达式模式,并阻止空格开始和结束字符串,并允许在 javascript 中的单词之间 - regEx pattern for blocking special characters fully, and blocking spaces start and end of the sting and allow in between words in javascript 正则表达式不允许在字符串开始或结束处使用“.”、“_”、“-”,并且不应有连续的“.” 其余的所有特殊字符都不应被允许 - Regex to not allow '.', '_', '-' at String Start or End and should not have consecutive '.' and the rest all special characters should not be allowed 正则表达式以匹配URL-字符串末尾的数字,但也匹配字符串开头的字符 - Regex to match URL - number at end of string, but also match characters at start of string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM