Page 1 of 1

Regular Expressions in JavaScript Only Basics discussed here Rate Topic: ***** 1 Votes

#1 singularity  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 184
  • Joined: 17-October 08

Posted 14 July 2010 - 10:08 AM

Regular expressions in Javascript:

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.
I was amused but his advice made me to think otherwise and I have been since then trying to dig deep in to this issue.

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!!


Is This A Good Question/Topic? 1
  • +

Replies To: Regular Expressions in JavaScript

#2 singularity  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 184
  • Joined: 17-October 08

Posted 17 August 2010 - 03:39 AM

Sorry guys the link is not working.
The Google definition for regular expressions is:

Quote

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. ...

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,657
  • Joined: 23-August 08

Posted 17 August 2010 - 05:32 AM

I removed the broken link. The board software does not like the word expressions in links for some reason and sticks a <b></b> tag immediately after expr. It's a known issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1