Closures in JavaScript

In this post, we mainly focus on Closures in a loop.

Sometimes it will be hard-to-spot bugs when it comes to Closures because everything looks great on the surface. For example, let us consider this function:

function F() {
    var arr = [], i;
    for (i = 0; i < 3; i += 1) {
        arr[i] = function () {
            return i;
        };
    }
    return arr;
}



In the above example, we are looping three times, each time creating a new function that returns the loop sequence number. The new functions will be added to an array and the array is returned at the end. So, let’s the run code..

> var arr = F();



Now you have an array of three functions and let’s invoke them by adding parentheses after each array element. The expected behavior is to see the loop sequence printed: 0, 1, 2. Let’s try:

> arr[0]();
3
> arr[1]();
3
> arr[2]();
3



We didn’t expect this result. Did we? So, what has happened there? All three functions pointed to the same local variable i. Why? The functions don’t remember values, they only keep a link (reference) to the environment variable where they were created. In this case, the variable i happens to live in the environment where the three functions were defined. So, all the functions, when they need to access the value, reach back to the environment and find the most current value of i. After the loop, the i variable’s value is 3. So, all the three functions point to the same value.

Why three and not two is another good question to think about for better understanding the for loop.

So, how do you implement the correct behavior? The answer is to use another Closure:

function F() {
    var arr = [], i;
    for (i = 0; i < 3; i += 1) {
        arr[i] = (function (x) {
            return function () {
                return x;
            };
        }(i));
    }
    return arr;
}



And this gives you the expected result.

> var arr = F();

> arr[0]();
0

> arr[1]();
1

> arr[2]();
2



Here, instead of creating a function that returns i, you pass the i variable’s current value to another immediate function. In this function, i becomes the local value of x, and x has different value every time.

Alternatively, you can use a normal (as opposed to an immediate) inner function to achieve the same result. The key is to use the middle function to localize the value of i at every iteration.

function F() {
    function binder(x) {
        return function () {
            return x;
        };
    }
    
    var arr = [], i;
    for (i = 0; i < 3; i += 1) {
        arr[i] = binder(i);
    }
    return arr;
}



So, when dealing the Closures in a loop, it is better to understand what’s running internally rather than focusing on the surface code.

-By
Uma Maheswar
Helical It Solution

Regular Expression

Regular Expression in Java

Regular Expression or popularly known as Regex is a powerful features and help developers in many ways.
The following are some special characters with their behaviour

. Match any character except newline

\w Match any alphanumeric character

\s Match any whitespace character

\d Match any digit

\b Match the beginning or end of a word

^ Match the beginning of the string

$ Match the end of the string

Repetitions

* Repeat any number of times

+ Repeat one or more times

? Repeat zero or one time

{n} Repeat n times

{n,m} Repeat at least n, but no more than m times

{n,} Repeat at least n times

Negation

\W Match any character that is NOT alphanumeric

\S Match any character that is NOT whitespace

\D Match any character that is NOT a digit

\B Match a position that is NOT the beginning or end of a word

[^x] Match any character that is NOT x

[^aeiou] Match any character that is NOT one of the characters aeiou

Construct Matches

[abc] Matches a, or b or c. This is called a simple class, and it matches any of the characters in the class.
[^abc] Matches any character except a, b, and c. This is a negation.
[a-zA-Z] Matches any character from a to z, or A to Z, including a, A, z and Z. This called a range.
[a-d[m-p]] Matches any character from a to d, or from m to p. This is called a union.
[a-z&&[def]] Matches d, e, or f. This is called an intersection (here between the range a-z and the characters def).
[a-z&&[^bc]] Matches all characters from a to z except b and c. This is called a subtraction.
[a-z&&[^m-p]] Matches all characters from a to z except the characters from m to p. This is also called a subtraction.

Examples

1. Username Regular Expression Pattern
^[a-z0-9_-]{3,15}$

2. Password Regular Expression Pattern
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})
(
(?=.*\d) # should have one digit from 0-9
(?=.*[a-z]) # should have one lowercase characters
(?=.*[A-Z]) # should have one uppercase characters
(?=.*[@#$%]) # should have one special symbols in the list "@#$%"
. # match anything with previous condition checking
{6,20} # length at least 6 characters and maximum of 20
)



3. Email Regular Expression Pattern
^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+

4 Image File Extension Regular Expression Pattern
([^\s]+(\.(?i)(jpg|png|gif|bmp))$)

5.Time in 24-Hour Format Regular Expression Pattern
([01]?[0-9]|2[0-3]):[0-5][0-9]

6. Date Format (dd/mm/yyyy) Regular Expression Pattern
(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)

Reference- internet