Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted December 15th, 2015
Regular Expressions

Regular expressions are a way to describe patterns in string data. They form a small, separate language that is part of JavaScript and many other languages and tools.Regular expressions are both terribly awkward and extremely useful. This is a powerful tool for inspecting and processing strings. The Expressions can be used for: searching, extracting, and modifying text.

How are regular expressions implemented in JavaScript?

  • Using literal syntax
  • Dynamically construct the regular expression, via the RegExp() constructor

The literal syntax looks something like:

var RegularExpression = /pattern/

RegExp() constructor method looks like

var RegularExpression  =  new RegExp("pattern");

The basic language constructs include character classes, quantifiers, and meta-characters.

Character Classes

Character classes are used to define the content of the pattern. E.g. what should the pattern look for?Set of character placed inside square brackets.
Range can be specified using β€˜-’.

.       Dot, any character
\d      A digit: [0-9]
\D      A non-digit: [^0-9]
\s      A whitespace character: [ \t\n\x0B\f\r]
\S      A non-whitespace character: [^\s]
\w      A word character: [a-zA-Z_0-9]
\W      A non-word character: [^\w]

Quantifiers

Quantifiers can be used to specify the number or length that part of a pattern should match or repeat. A quantifier will bind to the expression group to its immediate left.

*      Match 0 or more times
+      Match 1 or more times
?      Match 1 or 0 times
{n}    Match exactly n times
{n,}   Match at least n times
{n,m}  Match at least n but not more than m times

Meta-characters

Meta-characters are used to group, divide, and perform special operations in patterns.

\       Escape the next meta-character (it becomes a normal/literal character)
^       Match the beginning of the line
.       Match any character (except newline)
$       Match the end of the line (or before newline at the end)
|       Alternation (β€˜or’ statement)
()      Grouping
[]      Custom character class

Image of Regular Expressions

Examples : check input for 5 digit number-

<script>
function checkpostal(){
    var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number
    if (document.myform.myinput.value.search(re5digit)==-1) //if match failed
        alert("Please enter a valid 5 digit number inside form")
    }
</script>

<form name="myform">
<input type="text" name="myinput" size=15/>
<input type="button" onClick="checkpostal()" value="check"/>

</form>
var re5digit=/^\d{5}$/
  • ^ indicates the beginning of the string. Using a ^ metacharacter requires that the match start at the beginning.
  • \d indicates a digit character and the {5} following it means that there must be 5 consecutive digit characters.
  • $ indicates the end of the string. Using a $ metacharacter requires that the match end at the end of the string.

Posted by Silky Nanda

Leave a Reply

Your email address will not be published. Required fields are marked *