Regex match count of characters that are separated by non-matching characters

前端 未结 3 1810
悲&欢浪女
悲&欢浪女 2020-12-17 18:46

I want to count characters, but they may be separated by characters that do not match.

Here is an example. I want to match a text that has 10 or more word-characters

相关标签:
3条回答
  • 2020-12-17 19:19

    Use a group that consumes spaces with each single word char, and count the groups:

    ^(\s*\w){10,}\s*$
    
    0 讨论(0)
  • 2020-12-17 19:38

    Using JS: Remove the spaces, then do the \w check

    'foo baz barz'.replace(/ /g,'').match(/\w{10,}/) != null //true
    'foo bar baz'.replace(/ /g,'').match(/\w{10,}/) != null //false
    

    Match phone numbers in text:

    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"]
    
    0 讨论(0)
  • 2020-12-17 19:45

    Hey I think this would a simple but working one:

    ( *?[0-9a-zA-Z] *?){10,}
    

    Breaking the regex down:

    1. ( *? --------It can start with space(s)
    2. [0-9a-zA-Z] -Followed with the alphanumeric values
    3. *?) ---------It can end with space(s)
    4. {10,} -------Matches this pattern 10 or more times

    Key: When I look at the count for regexes, it applies to the group, i.e., the things in the brackets "()", this case, multiple spaces followed ONE from the alphanumeric values followed by spaces are still counted as one match. Hope it helps. :)

    0 讨论(0)
提交回复
热议问题