Introduction to javascript regular rxpression

1st Mar 2010 at 3:38 AM | Posted in Javascript regular expression | Leave a comment

Using literal syntax:
var reExample = /pattern/;
Using the RegExp() constructor:
var reExample = new RegExp(“pattern”)

test() & exec():
The regular expression method in JavaScript has two main methods for testing strings: test() and exec().
The exec() Method

The exec() method takes one argument, a string, and checks whether that string contains one or more matches of the pattern specified by the regular expression. If one or more matches is found, the method returns a result array with the starting points of the matches. If no match is found, the method returns null.
The test() Method

The test() method also takes one argument, a string, and checks whether that string contains a match of the pattern specified by the regular expression. It returns true if it does contain a match and false if it does not. This method is very useful in form validation scripts. The code sample below shows how it can be used for checking a social security number. Don’t worry about the syntax of the regular expression itself. We’ll cover that shortly.

Advertisements

My 1st Regular expression script

1st Mar 2010 at 3:37 AM | Posted in Javascript regular expression | Leave a comment

<html>
<head>
<title>Test</title>
<script type=”text/javascript”>
var RE_EXP =/^[0-9]{1}$/;

function checkSsn(ssn){
if (RE_EXP.test(ssn)) {
alert(“VALID”);
} else {
alert(“INVALID”);
}
}

</script>
</head>
<body>
<form onsubmit=”return false;”>
<input type=”text” name=”ssn” size=”20″>
<input type=”button” value=”Check”
onclick=”checkSsn(this.form.ssn.value);”>
</form>
</body>
</html>

Explaination:
var RE_EXP =/^[0-9]{1}$/;
1. A caret (^) at the beginning indicates that the string must start with this pattern.
( so ^foo is found in fooding but not in abcfood)

2. A dollar sign ($) at the end of indicates that the string must end with this pattern.
(so foo$ is found in abcfoo but not in abcfood)

3. Square brackets ( [] ) are used to group options.
(so the pattern f[aeiou]d can be found in “fad” and “fed”, but not in “food”, “faed” or “fd”.)
[0-9] means no character allowed only digits allowed. [a-zA-Z] means small a to z and capital A to Z.
Backslash-d ( \d ) also represents any digit. It is the equivalent of [0-9]. We can use [\d] in place of [0-9].
\w represents any word character (letters, digits, and the underscore (_) ).

[backslash ( \ ) is used to escape special characters. The pattern fo\.d can be found in “fo.d”, but not in “food” or “fo4d”. ]
4. Curly brackets with one parameter ( {n} ) indicate that the preceding character should appear exactly n times in the pattern.
(so The pattern fo{1}d can be found in “fod” , but not “food” or “fooood”.)

Curly brackets with two parameters ( {n1,n2} ) indicate that the preceding character should appear between n1 and n2 times in the pattern.
( so The pattern fo{2,4}d can be found in “food”,”foood” and “fooood”, but not “fod” or “foooood”.)

Curly brackets with one parameter and an empty second paramenter ( {n,} ) indicate that the preceding character should appear at least n times in the pattern.
(so The pattern fo{2,}d can be found in “food” and “foooood”, but not “fod”.)

Use var RE_EXP =/^[\d]{1,2}$/; for 2 digits.
Use var RE_EXP =/^[\d]{1,}$/; for more then one digit.

Use var RE_EXP =^\d{3}([\- ]?)\d{2}([\- ]?)\d{4}$

for number format like: 123-45-6789
123 45 6789
123456789
123-45 6789
123 45-6789
123-456789
[Explaination:
1. /^ Start of expression.
2. \d{3} Takes three digits.
3. ([\- ]?) After 3 digits takes ‘-‘ or a ‘space’ no ‘nothing’. A questionmark (?) indicates that the preceding character should appear zero or one times in the pattern.( so The pattern foo? can be found in “food” and “fod”, but not “faod”.).
4. After this \d{2} Takes 2 digits and \d{4} Takes 4 digits.
]

Email validation by regular expression

1st Mar 2010 at 3:35 AM | Posted in Javascript regular expression | Leave a comment

Regular expression for email validation is:

var RE_EXP =/^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/;
[Explanation:
1. Parentheses ( () ) are used to capture subpatterns.(So f(oo)?d can be found in “food” and “fd”, but not in “fod”).
2. Backslash-w ( \w ) represents any word character (letters, digits, and the underscore (_) ).
3. A plus sign (+) indicates that the preceding character should appear one or more times in the pattern. (so fo+ can be found in “fod”, “food” and “foood”, but not “fd”.).
w+ allows for one or more word characters.
(\w+[\-\.])* allows for a sequence of word characters followed by a dot or a dash. The * indicates that the pattern can be repeated zero or more times.
4. @ allows for a single @ symbol.
5. (\w+\.)+ allows for a sequence of word characters followed by a dot. The + indicates that the pattern can be repeated one or more times.
6. [A-Za-z]+ allows for one or more letters. This is the “com” or “gov” portion of the email address.
7. The dollar sign ($) says to end here.
]
Use: /^(\w+[\-\.])*\w+@(\w+\.)+([A-Za-z]{2,3})$/;

this takes two or three character after dot(.) at end.
Or use: var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

More In Regularexpression

1st Mar 2010 at 3:33 AM | Posted in Javascript regular expression | 1 Comment

Backslash-W ( \W ) represents any character except a word character.
The pattern fo\Wd can be found in “fo*d”, “fo@d” and “fo.d”, but not in “food”.

Backslash-s ( \s) represents any whitespace character (e.g, space, tab, newline, etc.).
The pattern fo\sd can be found in “fo d”, but not in “food”.

Backslash-S ( \S ) represents any character except a whitespace character.
The pattern fo\Sd can be found in “fo*d”, “food” and “fo4d”, but not in “fo d”.

The pipe ( | ) is used to create optional patterns.
The pattern foo$|^bar can be found in “foo” and “bar”, but not “foobar”.

Blog at WordPress.com.
Entries and comments feeds.