简体   繁体   English

检查字符串是否在字符串中的任何位置包含1-3个.-_字符

[英]Check if string contains between 1 and 3 characters of .-_ anywhere in string

I am trying to create a regex that checks a string and will match a dot, dash or underscore. 我试图创建一个正则表达式来检查字符串,并将匹配点,破折号或下划线。 I only want it to allow a maximum of 3 otherwise it should not match the string. 我只希望它最多允许3个,否则它不应该与字符串匹配。

For example if I enter qwerty-123-123-123 that is okay. 例如,如果我输入qwerty-123-123-123可以。

If I enter something-123_world that is okay. 如果我输入something-123_world没关系。

However if I enter qwerty-123-_whatever-something this should not match. 但是,如果我输入qwerty-123-_whatever-something该值将不匹配。

Current regex 当前正则表达式

My current regex matches the specific characters I want but I can't seem to figure out how to only allow 3 maximum. 我当前的正则表达式与我想要的特定字符匹配,但我似乎无法弄清楚如何只允许最多3个字符。 I thought {1,3} was the answer but that didn't seem to work. 我以为{1,3}是答案,但这似乎没有用。 I also had a look at ?= positive lookups but not sure if that's correct / even able to get it to do what I want. 我也看过?=正向查询,但不确定那是正确的,甚至不能让它做我想要的事情。

You may use 您可以使用

/^[^-_.]*(?:[-_.][^-_.]*){1,3}$/

See the regex demo 正则表达式演示

Details 细节

  • ^ - start of string ^ -字符串的开头
  • [^-_.]* - any 0+ chars other than - , _ and . [^-_.]* -除-_和之外的任何0+个字符.
  • (?:[-_.][^-_.]*){1,3} - one, two or three occurrences of (?:[-_.][^-_.]*){1,3} -出现一次,两次或三次
    • [-_.] - a - , _ or . [-_.] -a - _.
    • [^-_.]* - any 0+ chars other than - , _ and . [^-_.]* -除-_和之外的任何0+个字符.
  • $ - end of string. $ -字符串结尾。

The other option apart from Regex would be to use JavaScript! 除了Regex之外,另一个选择是使用JavaScript!

 let str1 = 'qwerty-123-123-123'; let str2 = 'something-123_world'; let str3 = 'qwerty-123-_whatever-something'; const regex = /[._-]/g; let min = 1; let max = 3; function validate(txt) { var len = txt.match(regex).length; if(len >= min && len <= max) return true; return false; } console.log(validate(str1) ? 'Valid' : 'Invalid'); console.log(validate(str2) ? 'Valid' : 'Invalid'); console.log(validate(str3) ? 'Valid' : 'Invalid'); 

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

相关问题 如何检查一个字符串是否包含任何字符JavaScript之间的空格? - How to check if a string contains any kid of whitespace between characters JavaScript? 使用javascript检查字符串是否包含字符串中的url - check if string contains url anywhere in string using javascript 检查字符串是否包含日文/中文字符 - Check whether a string contains Japanese/Chinese characters 如何检查字符串是否仅包含拉丁字符? - How to check if string contains Latin characters only? Javascript 正则表达式检查字符串是否包含重音字符 - Javascript Regex to check if a string contains accented characters 如何使用 javascript 检查字符串中的任何位置是否包含 url 并将其转换为锚标记 - How to check if string contains url anywhere in string using javascript and convert it to anchor tag javascript检查字符串是否仅包含字母数字字符+其他特殊字符 - javascript check if string contains only alphanumeric characters + other special characters 检查字符串中两个字符之间的长度 - Check length between two characters in a string 如何检查字符串是否包含字符和空格,而不仅仅是空格? - How can I check if string contains characters & whitespace, not just whitespace? 如何检查字符串是否仅包含js中的某些字符? - How to check if a string only contains certain characters in js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM