0% found this document useful (0 votes)
2 views

javascript notes2

The document provides an overview of JavaScript Regular Expressions (RegExp), detailing their syntax, modifiers, brackets, metacharacters, and quantifiers for text searching and manipulation. It also covers JavaScript error types and handling, as well as validation techniques for user input. Additionally, it explains how cookies work in JavaScript, including their creation, storage, and retrieval using the document.cookie property.

Uploaded by

sardarom9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

javascript notes2

The document provides an overview of JavaScript Regular Expressions (RegExp), detailing their syntax, modifiers, brackets, metacharacters, and quantifiers for text searching and manipulation. It also covers JavaScript error types and handling, as well as validation techniques for user input. Additionally, it explains how cookies work in JavaScript, including their creation, storage, and retrieval using the document.cookie property.

Uploaded by

sardarom9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaScript RegExp(Regular Expression)

Last Updated : 11 Jun, 2024


A regular expression is a character sequence defining a search pattern. It’s
employed in text searches and replacements, describing what to search for within a
text. Ranging from single characters to complex patterns, regular expressions
enable various text operations with versatility and precision.
A regular expression can be a single character or a more complicated pattern.
Syntax:
/pattern/modifiers;
Example:
let patt = /GeeksforGeeks/i;
Explanation :
/GeeksforGeeks/i is a regular expression.
GeeksforGeeks is the pattern (to be used in a search).
i is a modifier (modifies the search to be Case-Insensitive).
Regular Expression Modifiers can be used to perform multiline searches which
can also be set to case-insensitive matching:
Expressions Descriptions

g Find the character globally

i Find a character with case-insensitive matching

m Find multiline matching

Regular Expression Brackets can Find characters in a specified range


Expressions Description

[abc] Find any of the characters inside the brackets

[^abc] Find any character, not inside the brackets

[0-9] Find any of the digits between the brackets 0 to 9

[^0-9] Find any digit not in between the brackets

(x | y) Find any of the alternatives between x or y separated with |

Regular Expression Metacharacters are characters with a special meaning:


Metacharacter Description

\. Search single characters, except line terminator or newline.

\w Find the word character i.e. characters from a to z, A to Z, 0 to 9

\d Find a digit

\D Search non-digit characters i.e all the characters except digits

\s Find a whitespace character

\S Find the non-whitespace characters.

\b Find a match at the beginning or at the end of a word

\B Find a match that is not present at the beginning or end of a word.

\0 Find the NULL character.

\n Find the newline character.

\f Find the form feed character

\r Find the carriage return character

\t Find the tab character

\v Find the vertical tab character

Find the Unicode character specified by the hexadecimal number


\uxxxx
xxxxx

Regular Expression Quantifiers are used to define quantitiesoccurrence


Quantifier Description

n+ Match any string that contains at least one n


Quantifier Description

n* Match any string that contains zero or more occurrences of n

JavaScript errors and validation are crucial for creating robust web
applications. Here's a breakdown:

Errors in JavaScript
 Types of Errors:
 Syntax Errors: These occur when you write incorrect JavaScript code, like missing
semicolons or incorrect syntax.
 Reference Errors: These happen when you try to access a variable that hasn't been
declared or is out of scope.
 Type Errors: These occur when you perform an operation on a value of the wrong
type, like trying to add a number to a string.
 Range Errors: These happen when you provide an argument to a function that's
outside the allowed range.
 URI Errors: These occur when you use incorrect syntax with functions
like encodeURI() or decodeURI().
 Handling Errors:
 try...catch: This statement allows you to catch and handle errors gracefully.
JavaScript
try {
// Code that might throw an error
} catch (error) {
// Code to handle the error
console.error(error.message);
}

 throw: You can use the throw statement to create custom errors.
JavaScript
if (x < 0) {
throw new Error("x cannot be negative");
}

Validation in JavaScript
 Purpose:
Validation ensures that user input or data meets specific criteria before it's
processed.
 Common Validation Tasks:
 Checking if a field is empty.
 Checking if an email address is valid.
 Checking if a password meets certain criteria (length, special characters, etc.).
 Checking if a number is within a certain range.
 Methods for Validation:
 HTML5 Constraint Validation: HTML5 provides built-in validation attributes for form
elements.
Code
<input type="email" required>

 JavaScript Validation: You can write custom JavaScript code to validate input
fields.
JavaScript
function validateForm() {
let email = document.getElementById("email").value;
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
alert("Please enter a valid email address.");
return false;
}
return true;
}

 Libraries and Frameworks: Libraries like jQuery or frameworks like React and
Angular offer additional validation features.
Example (Simple Form Validation)
JavaScript
function validateForm() {
let name = document.getElementById("name").value;
if (name === "") {
alert("Please enter your name.");
return false;
}
return true;
}

 A cookie is information that a website puts on a user's computer. Cookies store


limited information from a web browser session on a given website that can then
be retrieved in the future. They are also sometimes referred to as browser cookies,
web cookies or internet cookies.

JavaScript Cookies

A cookie is an amount of information that persists between a server-side and a


client-side. A web browser stores this information at the time of browsing.

A cookie contains the information as a string generally in the form of a name-


value pair separated by semi-colons. It maintains the state of a user and
remembers the user's information among all the web pages.
How Cookies Works?

o When a user sends a request to the server, then each of that request is
treated as a new request sent by the different user.
o So, to recognize the old user, we need to add the cookie with the response
from the server.
o browser at the client-side.
o Now, whenever a user sends a request to the server, the cookie is added
with that request automatically. Due to the cookie, the server recognizes
the users.

How to create a Cookie in JavaScript?


In JavaScript, we can create, read, update and delete a cookie by
using document.cookie property.

The following syntax is used to create a cookie:

1. document.cookie="name=value";
JavaScript Cookie Example
Example 1
Let's see an example to set and get a cookie.

1. <!DOCTYPE html>
2. <html>
3. <head>
4. </head>
5. <body>
6. <input type="button" value="setCookie" onclick="setCookie()">
7. <input type="button" value="getCookie" onclick="getCookie()">
8. <script>
9. function setCookie()
10. {
11. document.cookie="username=Duke Martin";
12. }
13. function getCookie()
14. {
15. if(document.cookie.length!=0)
16. {
17. alert(document.cookie);
18. }
19. else
20. {
21. alert("Cookie not available");
22. }
23. }
24. </script>
25.
26. </body>
27. </html>

You might also like