Sunday, March 29, 2015

JavaScript regular expressions


Regular expressions is a form of pattern matching that you can apply on textual content.When you search for data in a text, you can use this search pattern to describe what you are searching for.

In JavaScript, regular expressions are often used with the two string methods: search(), replace(), match(), split().

Modifiers

Several modifiers are available that can make your work with regexps much easier, like case sensitivity, searching in multiple lines etc.

  • i -- Perform case-insensitive matching.
  • m -- Specifies that if the string has newline or carriage return characters.
  • g -- Perform a global match , find all matches rather that stopping after the match. 
Search :
The search() method uses an expression to search for a match, and returns the position of the match.


 Syntax : string.search(searchvalue)
   
                            searchvalue -- Required. A string will automatically be converted to a regular                                                              expression.
 ExampleTry it yourself

replace :
  The replace() method returns a modified string where the pattern is replaced.

Syntax : string.replace(searchvalue,newvalue)
         
                            searchvalue -- Required.The value or regular expression, that will be replaced buy                                                       the new value.
                             newvalue     -- Required.The value to replace the searchvalue with.

Example : Try it yourself

match :           
  The match() method searches a string for a match against a regular expression, and returns the matches, as an array object.

 Syntax : string.match(regexp)

                            regexp --  Required.The value to search for, as a regular expression.

Example : Try it yourself

split :
  The split() method is used to split a string into an array of substrings, and returns the new array.

Syntax : string.split(separator,limit)

                         separator -- Optional.Specifies the character, or the regular expression, to use for                                                     splitting the string. If omitted, the entire string will be returned.
                         limit        -- Optional. An integer that specifies the number of splits, items after the                                                    split limit will not be included in the array.

Example :  Try it yourself

Unknown Software Engineer

2 comments:

  1. Can u provide some samples of regular expressions, please ?

    ReplyDelete
  2. Thanks for the interest shiv . May i know that have you tried on " try it yourself" ?

    ReplyDelete