views:

259

answers:

6

Hi, i want to know how to write the regular expression in javascirpt please help me give a simple exaple with details i mean the source code(i am using asp.net and c# language) thank u

+1  A: 

You may want to refer concise article on : www.regular-expressions.info/javascript.html

First you need to understand the concept of regular expression. Once you know what regex is, writing them in any language is not difficult.

jatanp
As for me, regex is always difficult :)
x2
ya thanks mr.jatanp i think u r an Indian right?
Surya sasidhar
+1  A: 

You can read these

Regular Expressions

Regular Expressions patterns

String and Regular Expression methods

If you are interested in buying a regex book then this one is good

Regular Expressions Cookbook

rahul
Thank u for good reference Mr.Phoenix
Surya sasidhar
A: 

You can write a regular expression literal enclosed in slashes like this:

var re = /\w+/;

That matches something that contains one or more word characters.

You can also create a regular expression from a string:

var re = new RegExp("\\w+");

Notice that since that's a string literal, I have to double the backslash to escape its special meaning for strings.

Rob Kennedy
+1  A: 

There are tons of examples online for using JavaScript's RegExp object. Start with this.

Here is a simple example of creating a RegExp and then using it to determine whether there is at least one occurrence of the word "dog" in the passed string.

var myString = "I wish all dogs were brown.";
var myRegExp = new RegExp("dog");
var containsDog = myRegExp.test(myString);

In this example, containsDog would be 'true'.

Gene Goykhman
thank u Mr.Gene Goykhman i will check it out this
Surya sasidhar
Unless you are building a regex expression by concatenating strings, go with the short syntaxvar myRegExp = /dog/ // Equivalent to new RegExp("dog")and if you want to ignore case do like sovar myRegExp = /dog/i // Equivalent to new RegExp("dog", "i")and if you want to add the global parametervar myRegExp = /dog/g // Equivalent to new RegExp("dog", "g")You can also use bothvar myRegExp = /dog/ig // Equivalent to new RegExp("dog", "ig")
nickyt
+1  A: 

I think the following articles from Mozilla Dev Center are a very good introduction:

CMS
thank you for good reference Mr. CMS
Surya sasidhar
A: 

This should also work:

var myString = "I wish all dogs were brown.";
if (myString.match(/dog/i))
{
    //do something
}
Christoph