JavaScript - match() and exec()

Asked By Shilpa Sharma
10-Sep-10 04:19 PM
Hi,

Can anyone please explain me what is the difference between match() and exec() methods of javascript? I googled for it but not able to make out the difference and when should be used which method.

Thx in advance
Shilpa
  Venkat K replied to Shilpa Sharma
10-Sep-10 09:44 PM
Both match() and exec() works almost same, but the main different between these two function i know is:
match() returns an array or matches when passed a global regular expression and exec() in contrast always return a single match and provides complete information of that match.

Thanks
  [ Kirtan ] replied to Shilpa Sharma
11-Sep-10 12:04 AM

The match() method searches for a match between a regular expression and a string, and returns the matches.

This method returns an array of matches, or null if no match is found.

The exec() method tests for a match in a string.

This method returns the matched text if it finds a match, otherwise it returns null.


Create New Account
help
Need help with regular expression syntax .NET Framework Hi, I am trying to learn how to use JScript regular expressions. I have considerable VBScript experience, but little JScript knowledge. I started with the following Spain falls mainly in the plain."; var re = / \ w+ / g; var arr; while ((arr = re.exec(src)) ! = null) print(arr.index + "-" + arr.lastIndex + " \ t" + arr); } else{ alert("You need a newer ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion()) if (ver > = 5.5){ var qq = / \ w+ / g; var arr; while ((arr = qq.exec(sString)) ! = null) s + = arr.index + "-" + arr.lastIndex + " " + arr + " \ n"; } else{ alert("You need a newer further, so that I can pass the pattern and options to be used by the regular expression object I named qq in the function. My attempt fails - - the following script gives me var qq = / \ w+ / g; var qq = new RegExp(sPattern, sOptions); var arr; while ((arr = qq.exec(sString)) ! = null) s + = arr.index + "-" + arr.lastIndex + " " + arr + " \ n"; } else{ alert("You need a newer
Regular Expression i want a regular expression for accepting only character and numbers and / and - but it should not accept space plz can ny one give me d expression.? plz reply me urgently.plzzz You can check here for the regular expression for various patters on strings and use them. http: / / regexlib.com / DisplayPatterns.aspx?cattabindex = 3 categoryId = 4 If you are using Regular expression validator below is the one <asp:RegularExpressionValidator ID = "RegularExpressionValidator1" ValidationExpression = " ^[0-9a-zA-Z
Issue with regular expression in VS2008 Macro .NET Framework I've been working on a macro for several weeks inserts custom "trace" commands into every function and every "catch" block. Currently, I use a regular expression that searches the current CodeElement (the function) for "catch" blocks. The regex breaks the block the '{' group for that specific catch block. The problem with doing this is that my regular expression does not seem to take newlines into account and adds the number of newlines from specific ' 'CodeElement contains an exception block. . .specifically a 'catch' block. It 'starts by using a regular expression to search the CodeElement and return 'matches. After the list of matches has been populated
Regular Expression Question C++ / VB I want to get all the 4 digit numbers from the following RegExpFind(patrn, strng) Dim regEx, Matches ' Define variables. Set regEx = 3D New RegExp ' Create a regular expression. regEx.Pattern = 3D patrn ' Set pattern. regEx.IgnoreCase = 3D False ' Set case insensitivity. regEx.Global This was a highly interesting problem because I think it exposes a bug in VBScript Regular Expression object. In particular, the regular expression which finds the number of such matches is: (? = 3D \ d{4}) That is subject = 3D
Date Validation for textbox I want regular expression for accepting all date formats use this function sub isvaliddate { my $input = shift; if ($input you. Please refer this . this is not possible to validate all date format with single regular expression so for this type of vlaidation you need to solve by foloowing regular expression (([1-9] | 1[012])[- / .]([1-9] | [12][0-9] | 3[01])[- / .](19 | 20) \ d \ d Text = dtime.ToString( "mm / dd / yyyy" ); } I just got one idea, probably you can use Regular expression class provided by Javascript and .NET library to validate your dates. For e.g. you