Content

  • JavaScript Regular Expressions
  • JavaScript Errors
  • JavaScript Scope
  • JavaScript Use Strict
  • JavaScript this Keyword
  • JavaScript Arrow Function

JavaScript Regular Expressions

  • A regular expression is a sequence of characters that forms a search pattern.
  • When you search for data in a text, you can use this search pattern to describe what you are searching for.
  • A regular expression can be a single character, or a more complicated pattern.
  • Regular expressions can be used to perform all types of pattern match,text search and text replace operations.
  • The search() method uses an expression to search for a match, and returns the position of the match.
  • The replace() method returns a modified string where the pattern is replaced.
Example :
//syntax-/pattern/modifiers;
/revolution/i; //i  is a modifier (modifies the search to be case-insensitive).

//search
let text = "Visit Revolution";
let n = text.search(/revolution/i);

//replace
let text = "Visit Company!";
let result = text.replace(/company/i, "Revolution");
Regular Expression Modifiers
Modifier Description
i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping after the first match)
m Perform multiline matching
Regular Expression Patterns
Expression Description
[abc] Find any of the characters between the brackets
[0-9] Find any of the digits between the brackets
Example
let text = "Is this all there is?";
let result = text.match(/[h]/g); //output will be - h,h

let number = "123456789";
let res = text.match(/[1-4]/g);//output will be - 1,2,3,4
Metacharacters
Metacharacter Description
\d Find a digit
Example
let text = "Give 100%!"; 
let result = text.match(/\d/g); output - 1,0,0

Regular expression are also used to implement client side form validations

Example : JS Function to validate ASP.NET Webform
function valid() {
var name = document.getElementById('<%= this.txtname.ClientID %>').value;
var email = document.getElementById('<%= this.txtemail.ClientID %>').value;
var phone = document.getElementById('<%= this.txtmobile.ClientID %>').value;
var city = document.getElementById('<%= this.txtcity.ClientID %>').value;
mobilecon = /^\d{10}$/;
emailcon = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([com\co\.\in])+$/;
if (name == "" || email == "" || phone == "" || city == "") {
    swal("Please fill all details to proceed..!", "", "warning");
    return false;
}
if (phone != '') {
    if (!phone.match(mobilecon)) {
        swal("Please Enter Valid Contact Number", "", "info");
        return false;

    }
}
if (email != '') {
    if (!email.match(emailcon)) {
        swal("Please Enter Valid Email-Id", "", "info");
        return false;
    }
}
                        
        return true;
}

JavaScript Errors

Example
try 
{
adddlert("Welcome guest!");
}
catch(err) 
{
document.getElementById("demo").innerHTML = err.message;
}

JavaScript Scope


JavaScript Use Strict

In strict mode :

Example
"use strict";
x = 3.14;  // This will cause an error (x is not defined).

JavaScript this Keyword
Example
// Create an object:
const person = {
    firstName: "ABC",
    lastName: "PQR",
    id: 5566,
    fullName : function() {
    return this.firstName + " " + this.lastName;
    }
};

JavaScript arrow function

Arrow functions allow us to write shorter function syntax

Example :
//function
let function hello() {
    return "Hello World!";
  }

//arrow function
hello = () => {
    return "Hello World!";
  }

//arrow function with parameters
let myFunction = (a, b) => a * b;