Regular expressions has been a topic that I have found most developers trying to avoid, I remember once asking for some help in a forum about regular expressions in Javascript the helper advised me to get away with Javascript regular expressions
Quote
I always have issues with JS regexes so I've thrown them out for the most part and just make a stinking AJAX request and send them to a much more robust PHP regex I use now, but this one works and I use it some times.
Today I thought of sharing with you all the folks what I have so far learnt with regular expressions that will be of particular interest to Javascript developers.
(This tutorial is not complete, see part 2 soon)
Regular Expression Definition:
In computing, regular expressions, also referred to as regex or regexp, provide a concise and flexible means for matching strings of text such as particular characters, words, or patterns of characters.
Regular expressions are a powerful tool that can be used to pattern matching. With the help of Regular expressions that validation in Javascript becomes too trivial and I should not say easy rather challenging and interesting.
The general syntax for specifying a Javascript regular expression would be
var rex = /pattern/modifiers
Here pattern specifies the pattern or type of the regular expression e-g the pattern of email id is yourname@domain.com and not something that contains @ and . only.
Modifiers specify how we want out patters to be matched. e-g: putting i as a modifier means that we are interested in case insensitive match of our regular expression.
Pattern matching characters are numerous and hence are grouped in to various categories:
Position Matching
If you wish to match a certain character that happens to be at a specific position in a sequence. E-g a substring that may happen to be an initial character or say some five characters behind the start of sequence or say at the end of the sequence:
The following are the position matching characters available in Javascript
1: Caret(^): ^ matches only the beginning of the string. E-g:
a: /^singularity/ matches “singularity” in “singularity is yet to come ” and not “when is singularity gonna happen”
(The modifier i is used to signify a case insensitive match)
c: /^singularity/i matches “SinGUlaRiTy” or “sINgUlaRity” or “singularity” in “singularity is yet to come ” or “sIngUlariTy is yet to come ”and not “when is singularity gonna happen”
2: Dollar($): $ matches only the end of the string E-g:
a: /organ$/ matches “organ” in “microorgan” but not in “organismic”
3: Boundary Character(\b):\b matches any boundary character. i-e it should be either at the beginning or at the end of the string. E-g:
a: /ly\b/ matches “ly” in “This is really Cool.”
4: Non Boundary Character(\B): E-g:
a: /\Bor/ matches “or” in “normal” but not in “organisms”.
This is just a start with the Javascript regular expression. Treat this as a part 1 and I am coming back with the part 2 very soon.
Part 2 will be continuation of the same with a real example script to test a validate a test expression in Javascript using Javascript regular Expressions.
Cheers!!





MultiQuote




|