简体   繁体   English

正则表达式匹配第一个字符一次,然后重复匹配直到结束

[英]Regex match first character once, followed by repetitive matching until end

I'm trying to match characters that shouldn't be allowed in a username string to then be replaced.我正在尝试匹配用户名字符串中不应允许的字符然后被替换。

Anything outside this range should match first character [a-zA-Z] <-- restricting the first character is causing problems and I don't know how to fix it此范围之外的任何内容都应匹配第一个字符[a-zA-Z] <-- 限制第一个字符会导致问题,我不知道如何解决它

And then match everything else outside this range [0-9a-zA-Z_.] <---- repeat until the end of the string然后匹配此范围之外的所有其他内容[0-9a-zA-Z_.] <---- 重复直到字符串结尾

Matches:火柴:

  • /////hey/// <-- first match ///// , second match /// /////嘿/// <--第一个匹配////,第二个匹配///
  • [][123Bc_.// <-- first match [][ , second match // [][123Bc_.// <-- 第一个匹配[][ , 第二个匹配//
  • (/abc <-- should match (/ (/abc <-- 应该匹配(/
  • a2__./) <-- should match /) a2__./) <-- 应该匹配/)

Non Matches:不匹配:

  • a_____一个_____
  • b__... ……

Current regex当前的正则表达式

/^([^a-zA-Z])([^\w.])*/
const regex = /^([^a-zA-Z])([^0-9a-zA-Z_.])*/;
'(/abc'.replace(regex, '') // => return expected abc
'/////hey///'.replace(regex, '') // => return expected "hey"
/^([^a-zA-Z])([^\w.])*/

You can not do it this way, with negated character classes and the pattern anchored at the start.你不能这样做,使用否定的字符类和锚定在开头的模式。 For example for your va2__./) , this of course won't match - because the first character is not in the disallowed range, so the whole expression doesn't match.例如对于您的va2__./) ,这当然不匹配 - 因为第一个字符不在不允许的范围内,所以整个表达式不匹配。

Your allowed characters for the first position are a subset, of what you want to allow for “the rest” - so do that second part first, replace everything that does not match [0-9a-zA-Z_.] with an empty string, without anchoring the pattern at the beginning or end.第一个 position 允许的字符是您希望“其余”允许的子集 - 所以首先做第二部分,用空字符串替换所有不匹配[0-9a-zA-Z_.] ,而不在开头或结尾锚定模式。
And then, in the result of that operation, replace any characters not matching [a-zA-Z] from the start.然后,在该操作的结果中,从一开始就替换任何不匹配[a-zA-Z]字符。 (So that second pattern does get anchored at the beginning, and you'll want to use + as quantifier - because when you remove the first invalid character, the next one becomes the new first, and that one might still be invalid.) (所以第二个模式确实在开始时被锚定,并且您需要使用+作为量词 - 因为当您删除第一个无效字符时,下一个成为新的第一个,并且那个可能仍然无效。)

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

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