-2

I can't figure out this even after searching through examples of regex. Can someone tell me what this expression means?

 /.{2,}/

Thanks for your help

3 Answers3

4

Greedily match two or more of any characters that are not a line separator, but....

that will depend on your engine:

  • In PCRE, C#, Python, Ruby, the . matches any char that is not \n... Watch out, as that includes the carriage return \r
  • In JavaScript and Java, the . matches any char that is not a line break character (\n, \r, paragraph separators)...

{2,} is greedy (it eats up as many characters as possible)

Note that the {2,} quantifier is "greedy": .{2,} will match as many characters as possible, and will only "give back characters" (backtrack) if necessary, for instance to allow the next token (if any) to match.

DOTALL mode

In dotall mode (which you turn on differently in various engine), the . will also match line separators.

  • In most engines, this mode is activated by the s flag
  • In Ruby, it is activated by the m flag
Alan Moore
  • 68,531
  • 11
  • 88
  • 149
zx81
  • 38,175
  • 8
  • 76
  • 97
  • I appreciate your response. Basically I am trying to prevent customers injecting malicious code in my Customer Comment Form. The existing program only has /.{2,}/ in the Regex field. I would like to create a regex to only allow letters, numbers, commas,and periods. I would also like to limit the number of words to the comment area. So far I have this regex for characters allowed. /^[a-zA-Z0-9'.,\s\-&\(\)]*$/ Do I need to keep /.{2,}/ in the field? Please help me put "characters allowed and words limit together" or suggest better ones. I am grateful for your help! – user3807037 Jul 07 '14 at 06:43
  • Your regex has a `*` quantifier so it will allow an empty string. If you want 2 or more, or between 5 and 10, use `{3,}` or `{5,10}` – zx81 Jul 07 '14 at 06:48
  • @AlanMoore Thank you for inserting the missing `not` in the text. :) – zx81 Jul 08 '14 at 01:02
3

. matches any character except the newline character. Quoting from MDN's RegExp page,

(The decimal point) matches any single character except the newline character.

{2,} means two or more times.

So, the entire expression means that, "match any set of characters with length greater than or equal to 2"

thefourtheye
  • 206,604
  • 43
  • 412
  • 459
2

{ , } is a repetition quantifier. it is be used to specify how many times any token can be repeated against a successful match.

quoting from Limiting Repetition: (HERE)

The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite.
Thus, {2,} would mean 2 or more of the preceeding character.

the . represents any character (other than line separator) and the forward-slashes indicate the start and end of the regular expression.


UPDATE:

So far I have this regex for characters allowed. /^[a-zA-Z0-9'.,\s\-&()]*$/ Do I need to keep /.{2,}/ in the field?

{a,b} specifies minimum (through a) and maximum (through b) limits for a matching string. therefore, if you need to specify a min and/or max limit for your letters, numbers, commas,and periods you may enclose the regex for these characters in a group ( such as [ ]) and place { a, b} right after this group, like this: /^[a-z]{2,200}$/. This will match a string of 2 to 200 lowercase alphabets.

as for your letters, numbers, commas,and periods, in the regex /^[a-zA-Z0-9'.,\s\-&()]*$/ you'll need to escape . and replace * with {2,max-limit}, as following:
/^[a-zA-Z0-9'\.,\s\-&()]{2,20}$/

--> DEMO

sunbabaphu
  • 1,453
  • 1
  • 10
  • 15
  • I tried the code /^[a-zA-Z0-9'\.,\s\-&()]{2,20}$/ you gave me , but it did not limit the word count. The other parts of it work ok to filter unwanted characters. I appreciate your help so far !! – user3807037 Jul 08 '14 at 20:19
  • see the `DEMO` link in the answer. in that page, try to enter more than 20 characters and the result (on the bottom right side of the page) will **not** match anything. therefore a **null match** could at as a check for the text entered. between 2 and 20, both inclusive, the match returns a character. maybe, ***(insert `(` and `)` around the square brackets in the regex pattern)*** to allow grouping. maybe due to lack of **grouping** there's no return value from the match. – sunbabaphu Jul 08 '14 at 20:51