8.7. General QuantifiersA quantifier in a pattern means to repeat the preceding item a certain number of times. You've seen three quantifiers: *,+,and ?. But if none of those three suits your needs,use a comma-separated pair of numbers inside curly braces ({ }) to specify how few and how many repetitions are allowed. The pattern /a{5,15}/ will match from five to fifteen repetitions of the letter a. If the a appears three times,that's too few,so it won't match. If it appears five times,it's a match. If it appears ten times,that's still a match. If it appears twenty times,the first fifteen will match since that's the upper limit. If you omit the second number (but include the comma),there's no upper limit to the number of times the item will match. So,/(fred){3,}/ will match if there are three or more instances of fred in a row (with no extra characters,like spaces,allowed between each fred and the next). There's no upper limit,so that would match 88 instances of fred,if you had a string with that many. If you omit the comma as well as the upper bound,the number given is an exact count: //w{8}/ will match exactly eight word characters (occurring as part of a larger string,perhaps). And /,{5}chameleon/ matches "comma comma comma comma comma chameleon." By George,that is nice. The three quantifier characters that you saw earlier are common shortcuts. The star is the same as the quantifier {0,},meaning zero or more. The plus is the same as {1,meaning one or more. And the question mark Could be written as {0,1}. In practice,it's unusual to need any curly-brace quantifiers since the three shortcut characters are nearly always the only ones needed. |
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。