Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Regular Expressions in JavaScript

Rated 4.34 (Ratings: 11) ( Add your rating )

Log in to add a comment
(37 comments so far)

Want more?

 
Picture of liorean

David Andersson

Member info | Full bio

User since: January 14, 2002

Last login: March 29, 2007

Articles written: 1

What is a regular expression?

Regular expressions is a form of pattern matching that you can apply on textual content. Take for example the DOS wildcards ? and * which you can use when you're searching for a file. That is a kind of very limited subset of RegExp. For instance, if you want to find all files beginning with "fn" , followed by 1 to 4 random characters, and ending with "ht.txt" , you can't do that with the usual DOS wildcards. RegExp, on the other hand, could handle that and much more complicated patterns.

Regular expressions are, in short, a way to effectively handle data, search and replace strings, and provide extended string handling. Often a regular expression can in itself provide string handling that other functionalities such as the built-in string methods and properties can only do if you use them in a complicated function or loop.

RegExp Syntax

There are two ways of defining regular expressions in JavaScript — one through an object constructor and one through a literal. The object can be changed at runtime, but the literal is compiled at load of the script, and provides better performance. The literal is the best to use with known regular expressions, while the constructor is better for dynamically constructed regular expressions such as those from user input. In almost all cases you can use either way to define a regular expression, and they will be handled in exactly the same way no matter how you declare them.

Declaration

Here are the ways to declare a regular expression in JavaScript. While other languages such as PHP or VBScript use other delimiters, in JavaScript you use forward slash ( / ) when you declare RegExp literals.

Syntax Example
RegExp Literal
/ pattern / flags ; var re = /mac / i ;
RegExp Object Constructor
new RegExp(" pattern "," flags "); var re = new RegExp(window.prompt("Please input a regex."," yes|yeah ")," g ");

Flags

There are three flags that you may use on a RegExp. The multiline flag has bad support in older browsers, but the other two are supported in pretty much every browser that can handle RegExp. These flags can be used in any order or combination, and are an integral part of the RegExp.

Flag Description
Global Search
g The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.
Ignore Case
i The ignore case flag makes a regular expression case insensitive. For international coders, note that this might not work on extended characters such as å, ü, ñ, æ.
Multiline Input
m This flag makes the beginning of input ( ^ ) and end of input ( $ ) codes also catch beginning and end of line respectively.

Pattern

The patterns used in RegExp can be very simple, or very complicated, depending on what you're trying to accomplish. To match a simple string like "Hello World!" is no harder then actually writing the string, but if you want to match an e-mail address or html tag, you might end up with a very complicated pattern that will use most of the syntax presented in the table below.

Pattern Description
Escaping
\ Escapes special characters to literal and literal characters to special.

E.g: /\(s\)/ matches '(s)' while /(\s)/ matches any whitespace and captures the match.
Quantifiers
{ n }, { n ,}, { n , m }, *, +, ? Quantifiers match the preceding subpattern a certain number of characters. The subpattern can be a single character, an escape sequence, a pattern enclosed by parentheses or a character set.

{ n } matches exactly n times.
{ n ,} matches n or more times.
{ n , m } matches n to m times.
* is short for {0,} . Matches zero or more times.
+ is short for {1,} . Matches one or more times.
? is short for {0,1} . Matches zero or one time.

E.g: /o{1,3}/ matches 'oo' in "tooth" and 'o' in "nose".
Pattern delimiters
( pattern ), (?: pattern ) Matches entire contained pattern.

( pattern ) captures match.
(?: pattern ) doesn't capture match

E.g: /(d).\1/ matches and captures 'dad' in "abcdadef" while /(?:.d){2}/ matches but doesn't capture 'cdad'.

Note: (?: pattern ) is very badly supported in older browsers.
Lookaheads
(?= pattern ), (?! pattern ) A lookahead matches only if the preceeding subexpression is followed by the pattern, but the pattern is not part of the match. The subexpression is the part of the regular expression which will be matched.

(?= pattern ) matches only if there is a following pattern in input.
(?! pattern ) matches only if there is not a following pattern in input.

E.g: /Win(?=98)/ matches 'Win' only if 'Win' is followed by '98'.

Note: Support for lookaheads is lacking in most but the newest browsers.
Alternation
| Alternation matches content on either side of the alternation character.

E.g: /(a|b)a/ matches 'aa' in "dseaas" and 'ba' in "acbab".
Character sets
[ characters ], [^ characters ] Matches any of the contained characters. A range of characters may be defined by using a hyphen.

[ characters ] matches any of the contained characters .
[^ characters ] negates the character set and matches all but the contained characters

E.g: /[abcd]/ matches any of the characters 'a', 'b', 'c', 'd' and may be abbreviated to /[a-d]/ . Ranges must be in ascending order, otherwise they will throw an error. (E.g: /[d-a]/ will throw an error.)
/[^0-9]/ matches all characters but digits.

Note: Most special characters are automatically escaped to their literal meaning in character sets.
Special characters
^ , $ , . , ? and all the highlighted characters above in the table. Special characters mean characters that match something else than what they appear as.

^ matches beginning of input (or new line with m flag).
$ matches end of input (or end of line with m flag).
. matches any character except a newline.
? directly following a quantifier makes the quantifier non-greedy (makes it match minimum instead of maximum of the interval defined).

E.g: /(.)*?/ matches nothing or '' in all strings.

Note: Non-greedy matches are not supported in older browsers such as Netscape Navigator 4 or Microsoft Internet Explorer 5.0.
Literal characters
All characters except those with special meaning. Mapped directly to the corresponding character.

E.g: /a/ matches 'a' in "Any ancestor".
Backreferences
\ n Backreferences are references to the same thing as a previously captured match. n is a positive nonzero integer telling the browser which captured match to reference to.

/(\S)\1(\1)+/g matches all occurrences of three equal non-whitespace characters following each other.
/<(\S+).*>(.*)<\/\1>/ matches any tag.

E.g: /<(\S+).*>(.*)<\/\1>/ matches '<div id="me">text</div>' in "text<div id=\"me\">text</div>text".
Character Escapes
\f , \r , \n , \t , \v , \0 , [\b] , \s , \S , \w , \W , \d , \D , \b , \B , \c X , \x hh , \u hhhh \f matches form-feed.
\r matches carrriage return.
\n matches linefeed.
\t matches horizontal tab.
\v matches vertical tab.
\0 matches NUL character.
[\b] matches backspace.
\s matches whitespace (short for [\f\n\r\t\v\u00A0\u2028\u2029] ).
\S matches anything but a whitespace (short for [^\f\n\r\t\v\u00A0\u2028\u2029] ).
\w matches any alphanumerical character (word characters) including underscore (short for [a-zA-Z0-9_] ).
\W matches any non-word characters (short for [^a-zA-Z0-9_] ).
\d matches any digit (short for [0-9] ).
\D matches any non-digit (short for [^0-9] ).
\b matches a word boundary (the position between a word and a space).
\B matches a non-word boundary (short for [^\b] ).
\c X matches a control character. E.g: \cm matches control-M.
\x hh matches the character with two characters of hexadecimal code hh .
\u hhhh matches the Unicode character with four characters of hexadecimal code hhhh .

Usage

Now, knowing how a RegExp is written is only half the game. To gain anything from them you have to know how to use them too. There are a number of ways to implement a RegExp, some through methods belonging to the String object, some through methods belonging to the RegExp object. Whether the regular expression is declared through an object constructor or a literal makes no difference as to the usage.

Description Example
RegExp .exec( string )
Applies the RegExp to the given string, and returns the match information. var match = /s(amp)le/i.exec("Sample text")

match then contains ["Sample","amp"]
RegExp .test( string )
Tests if the given string matches the Regexp, and returns true if matching, false if not. var match = /sample/.test("Sample text")

match then contains false
String .match( pattern )
Matches given string with the RegExp. With g flag returns an array containing the matches, without g flag returns just the first match or if no match is found returns null. var str = "Watch out for the rock!".match(/r?or?/g)

str then contains ["o","or","ro"]
String .search( pattern )
Matches RegExp with string and returns the index of the beginning of the match if found, -1 if not. var ndx = "Watch out for the rock!".search(/for/)

ndx then contains 10
String .replace( pattern , string )
Replaces matches with the given string, and returns the edited string. var str = "Liorean said: My name is Liorean!".replace(/Liorean/g,'Big Fat Dork')

str then contains "Big Fat Dork said: My name is Big Fat Dork!"
String .split( pattern )
Cuts a string into an array, making cuts at matches. var str = "I am confused".split(/\s/g)

str then contains ["I","am","confused"]
Liorean is a twenty one years old medical student and hobbyist web designer mostly working with JavaScript and CSS, DOM and the newest html standards available. His personal dwelling on the net can be found at liorean@web-graphics.com .

Great article

Submitted by aggie on September 9, 2002 - 15:08.

This is officially my new reference for simple patterns, it's short and concise. I seldom use javascript for pattern matching, but most of this article applies to perl regex as well (I presume that's where JS got the idea). For those interested in going into a little more complex areas, such as recursive matching, once-only patterns and the weirder corners of perl style regexes might want to look at

<a href='http://www.perldoc.com/perl5.6/pod/perlre.html' target='_blank' name='Perl Docs on Regexes' alt='Perl Docs on Regexes'>http://www.perldoc.com/perl5.6/pod/perlre.html</a><br>(Perl Docs on regexes).

and for the PHP people,

<a href='http://www.php.net/manual/en/pcre.pattern.syntax.php' target='_blank' name='PHP docs on PCRE regexes' alt='PHP docs on PCRE regexes'>http://www.php.net/manual/en/pcre.pattern.syntax.php</a><br>(PHP docs on PCRE regexes)

cheers,

login or register to post comments

Terrific article

Submitted by sprocket on September 9, 2002 - 23:11.

Liorean,

Great article and a terrific complement to mine! I was wondering when this was going to be released. :) I can't wait to find a reason to do some regex in JS.

Aggie - you might be interested in my article as well - somewhat different info and writing style - http://www.evolt.org/RegEx_Basics .

Chris

login or register to post comments

Impressive

Submitted by notabene on September 10, 2002 - 04:14.

Clapping with both hands.

Concise, clear, everything I'm telling everyone but in a way they can understand ;-)

login or register to post comments

Other resources

Submitted by liorean on September 11, 2002 - 07:01.

Since Mozilla and Microsoft have slightly different RegExp implementations, you might want to look at their references as well:
Gecko based
DevEdge JavaScript 1.5 Core Reference - RegExp Object
Netscape 4.x
Old DevEdge JavaScript 1.3 Client Reference - RegExp Object
Microsoft
MSDN JScript 5.6 Reference - RegExp Object
Note that Microsoft has listed how it - according to them - SHOULD work, not what actually works. Even IE6 doesn't do everything MS claims it does. I've heard from friends that in ASP everything works as listed though.
// Liorean

login or register to post comments

Need some help

Submitted by jssingh on September 12, 2002 - 15:22.

I am trying to construct a reg exp for a field which can accept values from [A-Za-z0-9]. It cannot start with a number and cannot have only numbers. Its maximum length is 50. Apart from the above characters it can only include the symbols Underscore, Hyphen and spaces between words. Can someone help me create this reg exp, thanks!

login or register to post comments

Help is on the way!

Submitted by liorean on September 12, 2002 - 16:10.

Here's a regex that should be sufficient for your needs:
/^[a-z][-a-z0-9_ ]{0,49}$/i
Breakdown:
/^
Begin match at beginning of string
[a-z]
Match one alpha character
[-a-z0-9_ ]{0,49}
Match zero to fourtynine occurences of one of the following: alpha, digit, hyphen, underscore and space
$
Match end of string
/i
Append case insensitivity flag (Makes the regex smaller than the /^[A-Za-z][-A-Za-z0-9_ ]{0,49}$/ it would have been without case insensitivity flag)
// Liorean

login or register to post comments

Thanks

Submitted by jssingh on September 12, 2002 - 16:25.

hey that was quick. I much appreciate the help. Thanks a ton.

login or register to post comments

Match only outside ASP delimiters?

Submitted by redeye on October 10, 2002 - 20:18.

Killer article. Now, do you know if it's possible (not asking you to write the code) to replace occurrences of "&" but only outside of ASP or other delimiting characters?

login or register to post comments

Search for substring occurences indexes

Submitted by Ahmed on January 17, 2003 - 03:23.

Hello, I'd like to retrieve the indexes (as an array?) of a particular substring in a string through a single operation. For exemple: "get me all the indexes of the substring "" from the string "some textethis is some other html text". The ".search()" method only gives the index of the first occurence, while ".match()" only return substrings.

login or register to post comments

You'll have to make a function for it

Submitted by liorean on February 26, 2003 - 09:44.

Something like this would probably be needed:

var re=/\$\s?[0-9.,']+/g, str="I paid $1.25 for a cup of dices, I won $5.75 from Welma, "+ "but lost my car, clothes, and the $55.60 I had in my wallet to Shaggy. "+ "Good thing Fred lent me $100, or I wouldn\'t be able to pay the $35 "+ "I owed Daphne since I bought Scooby snacks on the internet. "+ "Oh, and that cursed dog ate all my Scooby snacks, too!"; String.prototype.getIndices=function(re){ var i=this.match(re), o=this.split(re), res=[]; for(var j=0,l=0;j<i.length;j++) res[res.length]=[(l+=o[j].length),(l+=i[j].length)]; return res; }; alert(str.getIndices(re).join(';')); // You'll get this list: 7,12;39,44;91,97;152,157;191,194

OK, this will give you not an array of indexes, but an array of arrays containing the index and lastindex of each occurence. If you wish to just get the indices, change the loop to:

for(var i=0,l=0;i>in.length;i++){ res[res.length]=(l+=o[j].length); l+=i[j].length; }

// liorean PS. Sorry for the delay in answering, but I've not been checking on this article for some time. I checked in on it just now since JavaScript Kit asked whether they could publish it too.

login or register to post comments

why can't i figure this out?

Submitted by Rajeewa on March 19, 2003 - 11:36.

Hi, Here's my problem: My input needs to allow only alpha chars and spaces. so i write --> /.[^a-z\s]/i but, noooooo! the test fails! i.e "1", "1a" returns false. bad bad bad! why does the test happen in pairs (ignoring the first char)? also, i need to put a condition that disallows a space as the first char too. i can't even get to this step. many thanks in advance... _R

login or register to post comments

Outputting delimiters using split()

Submitted by robertm on April 5, 2003 - 05:17.

According to http://www.webreference.com/js/column5/methods.html

document.write("1-10,20".split(/(-)|(,)/).join(":"));
produces the following output for Navigator 4.0x:
1:-:10::,:20
and the following output for Internet Explorer 4.0:
1:10:20

whereas

document.write("1-10,20".split(/([-,])/).join(":"));
produces the following:
1:-:10:,:20
(by implication, in both browsers)

Yet when I try the either pattern in IE6/Javascript1.2, I get
1:10:20
while what I actually want is to get the delimiters as well, i.e.
1:-:10:,:20

Can anybody tell me how I can get the delimiters out?

Thanks,
RobertM

login or register to post comments

Re: why can't i figure this out?

Submitted by notabene on April 5, 2003 - 07:54.

Rajeewa,

/.[^a-z\s]/i means:

  1. first character is any character
  2. second character is not an alpha and not a space (and I'm not even sure the syntax is good as is )
  3. string ends here (two characters long).

(if I'm wrong anybody feel free to correct me)

What you want is:

  1. "not a space" in your case really means "alpha only as the first character": [a-z]
  2. alpha or space: ([a-z]|\s)
  3. any following character matches previous rule: *

Thus the expression you should try (I think) is: /[a-z]([a-z]|\s)*/i

HTH s t e f

login or register to post comments

Backreference help please !

Submitted by lukestyle on June 18, 2003 - 13:41.

Why does this expression not work? - it's supposed to capitalize the first letter of each word..

var str = "hello how are you".replace(/(\b[a-z])/g, '\1'.toUpperCase());
document.write(str);

any help appreciated.. :)

login or register to post comments

Strong Password

Submitted by syd108 on June 18, 2003 - 15:07.

Hi: Very nice article. I was wondering if you could help me with the following regular expression

1) Should be atleast 7 Alphanumeric Characters Long
2) Must Contain both Upper and Lower Case Letters
3) Have digits and punctuation characters as well as letters 0-9, !@#$%^&*()_+=-[]\;',./{}|:"?

Will appreciate any pointers.

Regards
Syd

login or register to post comments

re: Backreference help please !

Submitted by notabene on June 19, 2003 - 00:14.

lukestyle,

First here's something to have the back reference work: don't use \1 but $1.

I tried then to capitalize like you wanted, but strangely with Mozilla as well as with IE the .toUpperCase() seems to do nothing:

        var str = "hello how are you".replace(/\b([a-z])/g, '$1'.toUpperCase());
document.write(str);
       

This gives the same string as a result. So I tried to concatenate it to other strings, just to check that the resulting back-referenced string was usable as a string, and the concatenation works:

        var str = "hello how are you".replace(/\b([a-z])/g, '!' + '$1'.toUpperCase() + '!');
document.write(str);
       

This gives "!h!ello !h!ow !a!re !y!ou", which means that the string does behave like a string, I suppose. Yet the upper-casing doesn't work. Beats me.

login or register to post comments

re re: Backreference help please !

Submitted by lukestyle on June 19, 2003 - 00:40.

I've found that with IE6, Mozilla 1.3, Opera 7, this works :

var str = "hello how are you".toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()}); document.write(str);

don't ask me how!

i wouldn't be able to use it on a website, as it would be cutting off too many users.... :(

login or register to post comments

re: Backreference help please !

Submitted by notabene on June 19, 2003 - 01:33.

Interesting.

That would mean that JS needs a kind of callback function, otherwise it won't work. Never heard about that, but it kind of seems logical. cf. http://www.php.net/preg_replace_callback for a PHP definition of a RegExp callback function.

login or register to post comments

Help with something similar to all this....

Submitted by hbrady on July 18, 2003 - 07:30.

I am using the RegExp function from this article (http://www.aspalliance.com/remas/Library/HighlightKeywords/) to highlight text in an article from the search results entered by a user. The problem is by making these words bold, if there are any hyperlinks within the article (the article is dynamic and can be straight text or a wild mix of HTML tags) then the replace function from that regular expression jacks up the HTML code. Is there a way to specify in my regular expression to not do the replace if the match occurs within HTML delimiters (e.g < and >)? Thanks for any help you can provide.

login or register to post comments

Does JS support recursive regex?

Submitted by saul11 on September 12, 2004 - 07:12.

Perl and PHP, for example, support recursive Regex,
does JS supports this too ?

Consider the problem of matching a string in parentheses,
allowing for unlimited nested parentheses. Without the use
of recursion, the best that can be done is to use a pattern
that matches up to some fixed depth of nesting. It is not
possible to handle an arbitrary nesting depth. Perl 5.6 has
provided an experimental facility that allows regular
expressions to recurse (amongst other things). The special
item (?R) is provided for the specific case of recursion.

\( ( (?>[^()]+) | (?R) )* \)

First it matches an opening parenthesis. Then it matches any
number of substrings which can either be a sequence of nonparentheses,
or a recursive match of the pattern itself
(i.e. a correctly parenthesized substring). Finally there is
a closing parenthesis.

login or register to post comments

Regular and cont4extual expressions

Submitted by liorean on September 12, 2004 - 08:27.

Well, no, they don't. The features listed here sums up pretty much the entirely of regex in JavaScript, only missing a few implementation specific things and just including the most basic type of string replacements. If you want to deal with nesting, your easiest method is likely to be a nested function call where you replace the contents of the innermost nesting with a token character while storing the cut-out part, then you repeat that until all nesting is eliminated. This technique isn't especially hard to use if you're crafty, but if you want ie5 support you might have to create one whole lot of code. If you don't care about ie5 support you can use String.replace with a callback function as it's second argument, given you have some outer variable to store the cutouts in. The trick is to work from the inner most nesting to the outer most nesting.

Oh, and to be picky, PERL doesn't have regular expressions any longer. PERL's "regular expressions" are actually contextual expressions. This is needed to give features such as this recursiveness you mention. It's also the reason why PERL is actually slower than some of the weaker regular expressions implementations.

login or register to post comments

A Javascript Oversight?

Submitted by sstromer on January 4, 2005 - 14:59.

I have been trying to write a simple regex to eliminate all characters in a number that are not numbers or a decimal. I believe logic would state the regex would look something like string.replace(/[^0-9\.]/g,'') but this does not work. Adding the \. to the carat'ed not statement seems to make the carat act as a 'first character' carat, instead of a 'not' carat, whereas something like /[^0-9a]/ does as I'd expect, as in not any number or the letter 'a'. Any ideas?

login or register to post comments

\D

Submitted by saul11 on January 5, 2005 - 00:18.

I think this is what you are searching for : string.replace(/[\D]*/g, '') where \D matches any non-digit (short for [^0-9]).
You might wanna use a regex testing tool.
I use the regulator that I think is the best there is, nevertheless it has a really 'unstable' editting bahavior. But when you get used to it, I think its the best you can get. (its free)

The regulator : http://sourceforge.net/projects/regulator/
Bugs (check here to see what I mean by unstable editting behavior) : http://sourceforge.net/tracker/?group_id=105210&atid;=640570

login or register to post comments

Help for Regular expression

Submitted by kkuldeep on January 19, 2005 - 10:21.

Hi Guys, I knwo it is simple but for my it is not working. I am trying to construct a reg exp for a field which can accept values from [A-Za-z0-9 and Hyphan and underscore symbols. Can someone help me create this reg exp, Thanks for your help in advance Kuldeep

login or register to post comments

reg exp for xml element name validation

Submitted by anakish1980 on February 16, 2006 - 07:11.

help me writing regular exp for xml name validation, which can be used even for non-english languages also

login or register to post comments

Call for Help : Reg Exp not working

Submitted by rajneeshgarg on February 23, 2006 - 08:00.

Hi, I need a regular expression for the following scenerio: Only numeric and float values are allowed. There can be maximun 6 digits before decimal point. If there is a decimal, there can be a maximun of 6 digits after decimal. The following regular expression /^\d{1,6}*\.?\d{0,6}$/ is giving javascript error. However, if I change it to /^\d*\.?\d{0,6}$/ it does not give javascript error, but does not give the intended result. Could u plz look into it and help me out Regards, Rajneesh Garg http://rgarg.blogspot.com/

login or register to post comments

Reg Exp Help

Submitted by andreit on March 10, 2006 - 21:31.

Does anyone know what would be the best way to replace all of the white space, does not matter if there is only one " " space or whole bunch of them. I also need a regural expression for replacing two characters at the end of a number. For example 11th, 3rh, 4th, 1st -- i need to have st,rd,th,st replaced. Please let me know if you have any ideas.

login or register to post comments

match ANY character?

Submitted by huttarl on May 12, 2006 - 15:28.

I've been googling around, but can't find this anywhere... how do I match *any* character? "." matches any character except newline. Would ".|\n" work? I tried "[.\n]" but apparently "." inside brackets is not special.

Btw, thanks for the info on using "?" to set non-greedy behavior. I was looking for this info earlier and couldn't find it in other references. I would be interested to know whether this feature is standard across browsers.

login or register to post comments

To replace all whitespace

Submitted by huttarl on May 12, 2006 - 15:38.

To replace all whitespace with nothing:

  foo.replace(/\s+/g,'')

To remove two letters following a string of digits:

  foo.replace(/(\d+)[snrt][tdh]/gi, '$1')

I'm not sure if the $1 has to be in quotes or not.

login or register to post comments

Strong Password

Submitted by aarti on May 18, 2006 - 07:21.

Hi all, Its indeed a very nice article. I need a small help for the regular expressions... This request was already posted here..

1) Should be atleast 7 Alphanumeric Characters Long
2) Must Contain both Upper and Lower Case Letters
3) Have digits and punctuation characters as well as letters 0-9, !@#$%^&*()_+=-[]\;',./{}|:"?

Looking forward for the response...

Regards,
Aarti

login or register to post comments

RegExp vs replace

Submitted by redraiduzz on January 24, 2007 - 18:02.

this is very good; I was unsure of one thing, though. I was trying to format a date (via onBlur method) that one would/could input a date format of mm/dd/yy - if it was between /50 and /99 the onBlur function would auto-poulate 19 before the /99 string portion. Otherwise, it would auto populate "20" in front of say, /02 for example. Making /98 /1998 or /02 /2002 in the date string. Would I use RegExp or simply the replace method for this? Please advise. Thanks!

login or register to post comments

RegExp vs replace

Submitted by redraiduzz on January 24, 2007 - 18:03.

this is very good; I was unsure of one thing, though. I was trying to format a date (via onBlur method) that one would/could input a date format of mm/dd/yy - if it was between /50 and /99 the onBlur function would auto-poulate 19 before the /99 string portion. Otherwise, it would auto populate "20" in front of say, /02 for example. Making /98 /1998 or /02 /2002 in the date string. Would I use RegExp or simply the replace method for this? Please advise. Thanks!

login or register to post comments

Regular Expression

Submitted by Roshu_Roshu on March 15, 2007 - 14:16.

hello All, My requirements are ,for User Name=> 1> It should be alphbetic and allow spaces in between . 2> First character should not be space or it should be character 3> It should allow single space between 2 words Eg: Roshni Syed Khan SO please any one give me pattern for regular expression. Pattern.compile{..................} please fill the ......... Thank you, Roshni

login or register to post comments

HI I have tried it. You

Submitted by mitulrastogi on June 11, 2007 - 10:01.

HI I have tried it. You should not have * when you specify the number of digits in the expression. Suppose u have specified like d{1,6}*, its not correct. You should not have * in this case. It should be d{1,6}

login or register to post comments

Your article saved my sanity!

Submitted by zaroundus on August 4, 2007 - 22:23.

I was trying to perform regex in Javascript and just couldn't get the syntax correct until I found this. Thanks for the great read!

login or register to post comments

Hi everybody

Submitted by magstudios on August 7, 2007 - 07:24.

I was trying to perform regex in Javascript and just couldn't get the syntax correct until I found this. Thanks for the great read!

login or register to post comments

calendar details displaying

Submitted by see2782 on August 7, 2007 - 08:13.

hi, thanks for the calendar script. it saved lot of pain. but can anyone tell me how do i display the event details below the calendar. the link is opening in new page. this would be of great help. its urgent

login or register to post comments

The access keys for this page are : ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.