Read, With the Name of Your Lord Who Created

Javascript Regular Expressions

Posted by triaslama on February 23, 2008

Regular expressions (Regex) are patterns that used to match characters combination in strings. Creating regular expression in Javascript can be accomplished in two ways:

  1. Using regular expression literal, The literal expression must be placed inside slash characters (/<expression>/), ex: regEx = /tr*i+/.
  2. Using the RegExp object, ex: regEx = new RegExp(“tr*i+”).

Regular expressions has several special characters, the following are list of special characters and its description:

No.

Character

Meaning

1.

\

For characters that treated literally, indicates that the next character is special and should not be treated literally. (Expression /d/ literally match the character ‘d’, by placing a backslash in front of d (/\d/) the character become special mean to one digit character).

For characters that treated specially, indicates that the next character should be treated literally. (Character ‘*’ is special and means zero or more occurances of the preceding character to be matched. /t*/ means zero or more t characters, by preceding backslash in front of ‘*’ (‘/\*/’) it mean character ‘*’).

2.

^

Matches the beginning of input.

3.

$

Matches the end of input.

4.

*

Matches the preceding character zero or more times.

5.

+

Matches the preceding character one or more times.

6.

?

Matches the preceding character zero or one time.

7.

(x)

Matches ‘x’ and remembers the match.

8.

(?:x)

Matches ‘x’ but doesn’t remember the match.

9.

x(?=y)

Matches ‘x’ only if ‘x’ is followed by ‘y’.

10.

x(?|y)

Matches ‘x’ only if ‘x’ is not followed by ‘y’.

11.

x|y

Matches either ‘x’ or ‘y’.

12.

{n}

n is a positive integer. Matches exactly n occurances of the preceding characters.

13.

{n,}

Where n is a positive integer. Matches at least n occurances of the preceding characters.

14.

{n,m}

Where n and m are positive integer. Matches at least n and at most m occurances of the preceding character.

15.

[xyz]

A character set. Matches any one of the enclosed characters.

16.

[^xyz]

A negated or complement of character set. Matches anything that is not enclosed in the bracket.

17.

[\b]

Matches a backspace.

18.

\b

Matches a word boundary.

19.

\B

Matches a non word boundary.

20.

\cX

Where X is a control character. Matches a control character in a string.

21.

\d

Matches a digit character (range 0…9).

22.

\D

Matches any of non digit character. Equivalent to [^0…9].

23.

\f

Matches a form feed.

24.

\n

Matches a line feed.

25.

\r

Matches a carriage return.

26.

\s

Matches a single white space character, including tab, form feed, line feed.

27.

\S

Matches a single character other than white space.

28.

\t

Matches a tab.

29.

\v

Matches a vertical tab.

30.

\w

Matches any alphanumeric character including the underscore.

31.

\W

Matches any non word character.

32.

\n

Where n is a positive integer. A back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses).

33.

 

Matches a null character (don’t follow with another digit).

34.

\xhh

Matches the character with the code hh (two hexadecimal digits).

35.

\uhhhh

Matches the character with the code hhhh (four hexadecimal digits).

So how we test that a string matches a specified character? The regular expression object or literal in Javascript support several method, one of them is test() -I think it will be nice if I posting about the Javascript regex methods, hopefully I can accomplished that-. Look at the following simple example on how we identify is that an input mathes the following expression:^ tr*i+$ (this expression means start with ‘t’ character, following by zero or more ‘r’ character, and ended by at least one ‘i’ character).

<html>
<head>
<title>Test Javascript Regex</title>
<script lang=”javascript” type=”text/javascript”>
var expression = /^tr*i+$/; // here we use the literal JS regex.

function submitRegExp(input,e)
{
if (e.keyCode==13) {
var result = expression.test(input);
window.alert(result);
}
}

function testRegex(input)
{
var result = expression.test(input);
window.alert(result);
}
</script>
</head>
<body>
<!–Something about javascript regular expressions–>
<input type=”text” id=”txt” onkeydown=”submitRegExp(this.value,event)” />
<input type=”button” id=”btn” value=”Test Regex” onclick=”testRegex(txt.value)” />
</body>
</html>

The above code will produce a textbox and a button inside a browser. Type something in the textbox then hit enter key or press the button, an alert box will appear and tell to us whether our input match with the /tr*i+/ regular expression.

2 Responses to “Javascript Regular Expressions”

  1. disneyland said

    disneyland…

    […]Javascript Regular Expressions « Read, With the Name of Your Lord Who Created[…]…

  2. I like the helpful information you provide in your articles.
    I’ll bookmark your blog and check again here regularly. I am quite sure I’ll
    learn many new stuff right here! Good luck for the next!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: