简体   繁体   English

限制字符串大小RegEx

[英]Limit string size RegEx

I'm trying to match all the expressions where the string length is greater than 1. This is my regex. 我正在尝试匹配字符串长度大于1的所有表达式。这是我的正则表达式。

string.match(/\B\w+(?=\w)/gi);

I tried with this: 我试过这个:

string.match(/^\B\w+(?=\w){2,}$/gi);

But it doesn't work... 但它不起作用......

If you want to match every word longer than one character - use the quantifier - \\w{2,} . 如果要匹配长于一个字符的每个单词 - 请使用量词 - \\w{2,}

 var str = "ab cd efg hijk", res = str.match(/\\w{2,}/g); console.log(res); 

Another possible solution would be splitting the array and filtering out every word longer than one character. 另一种可能的解决方案是拆分数组并滤除长于一个字符的每个字。

 var str = "ab cd efg hijk", res = str.split(' ').filter(v => v.length > 1); console.log(res); 

Try this. 尝试这个。 Any string with length more than 1. 任何长度大于1的字符串。

.{2,}

Test it on https://regex101.com/ https://regex101.com/上测试

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

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