我想计算字符数,但它们可能被不匹配的字符分开 .
这是一个例子 . 我想匹配一个包含10个或更多单词字符的文本 . 它可能包括空格,但我不想计算空格 .
不应该匹配:"foo bar baz"(应该算9)不应该匹配:"a a"(应该算2)应匹配:"foo baz bars"(应计数10,匹配整个字符串)
这就是我提出的,但它重要的是:
((?<=\s)*\w(?=\s)*){10}
Edit 我不想要包含空格来计算 . 对不起,我编辑了几次,我没有正确描述 .
有什么想法吗?
嘿,我认为这将是一个简单但有效的方法:
( *?[0-9a-zA-Z] *?){10,}
打破正则表达式:
( *? --------It can start with space(s)
[0-9a-zA-Z] -Followed with the alphanumeric values
*?) ---------It can end with space(s)
{10,} -------Matches this pattern 10 or more times
密钥:当我查看正则表达式的计数时,它适用于该组,即括号中的内容“ () ”,这种情况下,多个空格后跟一个字母数字值后跟空格仍被计为一个匹配 . 希望能帮助到你 . :)
()
使用每个单词char使用空格的组,并计算组:
^(\s*\w){10,}\s*$
Using JS: 删除空格,然后执行 \w 检查
\w
'foo baz barz'.replace(/ /g,'').match(/\w{10,}/) != null //true 'foo bar baz'.replace(/ /g,'').match(/\w{10,}/) != null //false
匹配文本中的电话号码:
var test = 'something foo baz barz 07999-777-111 and 01234 567890 01234567890 some more'.match(/((\(?0\d{4}\)?[ -]?\d{3}[ -]?\d{3})|(\(?0\d{3}\)?[ -]?\d{3}[ -]?\d{4})|(\(?0\d{2}\)?[ -]?\d{4}[ -]?\d{4}))([ -]?\#(\d{4}|\d{3}))?/g); //result: ["07999-777-111", "01234 567890", "01234567890"]
3 回答
嘿,我认为这将是一个简单但有效的方法:
打破正则表达式:
( *? --------It can start with space(s)
[0-9a-zA-Z] -Followed with the alphanumeric values
*?) ---------It can end with space(s)
{10,} -------Matches this pattern 10 or more times
密钥:当我查看正则表达式的计数时,它适用于该组,即括号中的内容“
()
”,这种情况下,多个空格后跟一个字母数字值后跟空格仍被计为一个匹配 . 希望能帮助到你 . :)使用每个单词char使用空格的组,并计算组:
Using JS: 删除空格,然后执行
\w
检查匹配文本中的电话号码: