Regular Expression In JavaScript

enter In this tutorial we are going to focus on using regular expression in javascript.


source Regular expressions are patterns used to match character combinations in strings. In javascript we can use regular expression as an object too. With the exec and test method of RegExp we can invoke regular expression.

Creating Regular Expression:

enter The easiest way to create a new RegExp object is to simply use the special regex syntax: myregexp = /regex/

write my essay australia There is also another way that you can try: If you have the regular expression in a string, you can use the RegExp constructor:

Writing regular expressions: myregexp = new RegExp(regexstring).

cheapest essays online A regular expression pattern is composed of simple characters, such as /abc/, or a combination of simple and special characters, such as /ab*c/.
Simple patterns are constructed to find a direct match to a string. For example, the pattern /abc/ matches character set in strings when exactly the characters ‘abc’ occur together and in that order. For example, the pattern “Did you brought your abc notebook?” and “The labcall consultancy is hiring for javascript users.” In both cases the match is with the substring ‘abc’. There is no match in the string ‘lab tech’ because while it contains the substring ‘ab c’, it does not contain the exact substring ‘abc’.
As shown in previous example we can’t find characters in case of ‘lab tech’ with a simple pattern approach. For this kind of situation we hav to take help from a special character. If the pattern includes special characters. For example, the pattern /ab*c/ matches any character combination in which a single ‘a’ is followed by zero or more ‘b’s (* means 0 or more occurrences of the preceding item) and then immediately followed by ‘c’. In the string “babbcdabc,” the pattern matches the substring ‘abbc’.
So watching above example if you are thinking that there should be many special characters, then I would say you are right. In the following we are going to learn the purpose of those special characters. enter site Special Characters

go next Meanings


Case-insensitive matching


A global match


Multiline matching


A RegExp method that executes a search for a match in a string. It returns an array of information.


A RegExp method that tests for a match in a string. It returns true or false.


A String method that executes a search for a match in a string.


A string method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.


A string method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.


A string method that uses a regular expression or a fixed string to break a string into an array of substrings.


Find any character between the brackets


Find any character NOT between the brackets


Find any digit between the brackets


Find any digit NOT between the brackets


Find any of the alternatives specified


This symbols follow many rules. They are:

1. A backslash that precedes a non-special character indicates that the next character is special and is not to be interpreted literally.

2. A backslash that precedes a special character indicates that the next character is not special and should be interpreted literally.

3. Do not forget to escape \ itself while using the regexp(“pattern”) notation because \ is also an escape character in strings.


Beginning of input.


End of input.


The preceding character 0 or more times.


The preceding character 1 or more times.


The preceding character 0 or 1 time. If used immediately after any of the quantifiers *, +, or {}, matches the fewest possible characters.


Any single character except the newline character.


‘X’ and remembers the match. The parentheses are called essays on military service capturing parentheses.


‘X’ but does not remember the match. The parentheses are called help writing personal statement university non-capturing parentheses.


‘X’ only if ‘x’ is not followed by ‘y’. This is called a go site negated look ahead.


Either ‘x’ or ‘y’.


Exactly n occurrences of the preceding character. N must be a positive integer.


n and m are positive integers. Matches at least n and at most m occurrences of the preceding character. When m is zero, it can be omitted.


A word boundary.


A non-word boundary.


A single white space character.


A single character other than white space.


Indicates a tab.


Indicates a vertical tab.

In the following is a working example of regular expression:

<!DOCTYPE html>
<title>JavaScript Tutorial</title>
<SCRIPT LANGUAGE="JavaScript"><!--
function demoMatchClick() {
  var regexpr = new RegExp(document.demoMatch.regex.value); //acccepting expression
  if (document.demoMatch.subject.value.match(regexpr)) { //matching with given document
    alert("Successful match"); //showing result
  } else {
    alert("No match");

function demoShowMatchClick() {
  var regexpr = new RegExp(document.demoMatch.regex.value);//acccepting expression
  var m = regexpr.exec(document.demoMatch.subject.value);
  if (m == null) {
    alert("No match");
  } else {
    var s = "Match at position " + m.index + ":\n";
    for (i = 0; i < m.length; i++) {
      s = s + m[i] + "\n";

function demoReplaceClick() {
  var regexpr = new RegExp(document.demoMatch.regex.value, "g");
  document.demoMatch.result.value =
// -->
<h1>Form Validation</h1>

<FORM ID="demoMatch" NAME="demoMatch" METHOD=POST ACTION="javascript:void(0)">
<P>Regexp: <INPUT TYPE=TEXT NAME="regex" VALUE="Give Expression " SIZE=50></P>
<P>Subject string: <INPUT TYPE=TEXT NAME="subject"
VALUE="This is a test of the JavaScript RegExp object" SIZE=50></P>
<P><INPUT TYPE=SUBMIT VALUE="Test Match" ONCLICK="demoMatchClick()">
<INPUT TYPE=SUBMIT VALUE="Show Match" ONCLICK="demoShowMatchClick()"></P>

<P>Replacement text: <INPUT TYPE=TEXT NAME="replacement" VALUE="Give Replacement" SIZE=50></P>
<P>Result: <INPUT TYPE=TEXT NAME="result"
VALUE="click the button to see the result" SIZE=50></P>
<P><INPUT TYPE=SUBMIT VALUE="Replace" ONCLICK="demoReplaceClick()"></P>


1 thought on “Regular Expression In JavaScript

Leave a Reply

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