简体   繁体   English

REGEX 如何检查一个字符串中是否有超过 2 个空格?

[英]REGEX How can I check if a string has more than 2 spaces in it?

I am trying to regex (ECMAscript) check if a string has three or more spaces in it.我正在尝试使用正则表达式 (ECMAscript) 检查字符串中是否包含三个或更多空格。 Essentially, I want the minimum number of spaces to be 3 before it matches.本质上,我希望在匹配之前的最小空格数为 3。 Here are some examples;这里有些例子;

"Microsoft and Apple" This would NOT match “微软和苹果”这不匹配

"The quick brown fox" This WOULD match “The quick brown fox”这会匹配

"lots of spaces in this sentence" This WOULD match “这句话中有很多空格”这将匹配

This is the best I have so far;这是我迄今为止最好的;


/[\s]{3,}/gm /[\s]{3,}/gm


If the 'string' can contain everything (characters and numbers and special chars):如果“字符串”可以包含所有内容(字符和数字以及特殊字符):

/^((.*?)? (.*?)?){3,}?$/gm

This may not be optimally efficient, but you could split on spaces and then check the resulting length:这可能不是最有效的,但您可以按空格split ,然后检查结果长度:

 const x = [ "Microsoft and Apple", "The quick brown fox", "lots of spaces in this sentence", ] x.forEach(s => console.log(s, s.split(' ').length > 3))

Put three spaces in the regex, and match anything else around them在正则表达式中放置三个空格,并匹配它们周围的任何其他内容

.*.*.*.*

A regex that makes sure there at least 3 whitespaces in a string is确保字符串中至少有 3 个空格的正则表达式是

/^(?:\S*\s){3}/

See the regex demo .请参阅正则表达式演示 Details :详情

  • ^ - start of string ^ - 字符串的开始
  • (?:\S*\s){3} - three occurrences of zero or more non-whitespace chars and then a whitespace. (?:\S*\s){3} - 零个或多个非空白字符出现三次,然后是一个空白。

See the JavaScript demo:请参阅 JavaScript 演示:

 const texts = ['Microsoft and Apple','The quick brown fox','lots of spaces in this sentence']; const re = /^(?:\S*\s){3}/; for (const text of texts) { console.log(text, '=>', re.test(text)); }

The pattern /[\s]{3,}/gm matches 3 continuous whitespace characters, and \s can also match newlines.模式/[\s]{3,}/gm匹配 3 个连续的空白字符, \s也可以匹配换行符。

Note that you don't need /m as the pattern has no anchors, but you do need /g to get all matches.请注意,您不需要/m因为该模式没有锚点,但您确实需要/g才能获得所有匹配项。

You can match all spaces您可以匹配所有空格 and only get the string that has more than 2 spaces (or if you really want to use \s then use that)并且只获取超过 2 个空格的字符串(或者如果你真的想使用\s然后使用它)

 [ "Microsoft and Apple", "The quick brown fox", "lots of spaces in this sentence" ].forEach(s => { if ((s.match(/ /g) || []).length > 2) { console.log(s); } });

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

相关问题 如何使用正则表达式无方法检查字符串是否具有3个或更多小数点 - How to check if a string has 3 or more decimal points using regex and no methods 如何检查字符串是否包含多于1个整数,以逗号分隔 - How to check if a string has more than 1 integer, separated by a comma 我如何检查oncomplete内的两个以上条件 - How can I check more than two conditions inside oncomplete 如何使用columntocheck命令检查多个列? - How can I check more than one column with columntocheck command? 如何检查它是否包含多个内容? - How can I check if it includes more than one things? 正则表达式 - 检查字符串有一个或多个字符,没有一些字符 - Regex - check string has one or more chars without some chars 如何检查字符串是否在Javascript中的行中具有多个特定字符 - How to check if a string has more than one of a certain character in a row in Javascript 如何检查某人是否喜欢过一个帖子,所以他们不能多次喜欢它? - How to check if someone has liked a post so they can't like it more than once? 替换时如何检查匹配的字符在字符串中是否有空格 - How to check the matched character has spaces around in string when replacing it JavaScript Regex-如何检查以逗号分隔的值是否超过三个 - JavaScript Regex - How to check comma separated values are more than three
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM