Regular Expressions: An Introduction
by Amit Arora
Published: January 12, 2004
User Rating: 8.9 (274 votes)

Page Page » 1 2 3 4 5 6 7 8 9

Syntax

So taking the example above, we would say the syntax of regular expression is

/pattern/modifier

where

identifier The pattern is always enclosed in a identifier. The most common identifier is the slash (/).

pattern Then there is the pattern, which you want to find and replace.

modifier And then there is the modifier. Modifier is like an option for the pattern, which define extra properties of the pattern matching.
For e.g. using the case-insensitive match modifier (i)

Patterns

To read (understand) or write a regular expression pattern, you should ask the three simple question

  1. What characters to match ?
  2. How many characters to match ?
  3. And where to match ?

And therein lies the power and simplicity of the regular expression. Taking few examples

Wildcard * Regex /.*/ Explaintaion Match any character, any number of times, anywhere (in simple terms, match anything) Wildcard ? Regex /.?/ Explaintaion Match any character, 0 or 1 time, anywhere (in simple terms, match any character) Wildcard a? Regex /a.?/ Explaintaion Match "a" then match any character, 0 or 1 time, anywhere Wildcard *a. Regex /.*a$/ Explaintaion Match any character, any number of times, anywhere, then match "a" at the end of the subject. So in simple terms, match a string of any character which ends in "a" Wildcard *a. Regex /.*a$/i Explaintaion (Case-insesitive). Same as above, but matches "a" and "A"
Page Page » 1 2 3 4 5 6 7 8 9

About

Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

Contact | Resume

Subscribe to newsletter




Get Firefox

Monitored by Site24x7
Uptime