SlideShare a Scribd company logo
www.webstackacademy.com
Type Conversion & Regular Expressions
JavaScript
www.webstackacademy.com
 Type Conversion
 Regular Expression
Table of Content
www.webstackacademy.com ‹#›www.webstackacademy.com
Type Conversion
(JavaScript)
www.webstackacademy.com
Type Conversion
JavaScript variables can be converted to a new variable and
another data type:
 By the use of a JavaScript function
 Automatically by JavaScript itself
www.webstackacademy.com
Converting Number to String
 The String() and toString() can convert number to strings
String(234); //234
var n=45;
var x=n.toString(); // Result x is 45
www.webstackacademy.com
Converting Booleans to String
The String() and toString() can convert boolean to string.
String(false); // return false
false.toString(); // return false
www.webstackacademy.com
Converting Date to String
The String() and toString() can convert date to string.
String(Date());
// or
Date().toString();
www.webstackacademy.com
Converting String to Numbers
Option Description
Number() Convert Strings to numbers
parseFloat() Parses a string and returns a floating point number
parseInt() Parses a string and returns an Integer
www.webstackacademy.com
Unary +Operator
The unary + operator can be used to convert a variable to a number.
<body>
<button onclick="checkType()">Click this button</button>
<p id="ex"></p>
<script>
function checkType() {
var y = "5";
var x = + y;
document.getElementById("ex").innerHTML = typeof y + "<br>" + typeof x;
}
</script>
www.webstackacademy.com
Automatic Type Conversion
<script>
document.write((3 + null )+ "<br>"); //returns 3
document.write(("3" + null) + "<br>");//returns 3null
document.write("3" + 2 + "<br>");//returns 32
document.write(("3" - 2) + "<br>"); //returns 1
document.write(("5" * "2") + "<br>"); // returns 10
</script>
www.webstackacademy.com
Exercise
 Revisit the rectangle program (area & perimeter). Check the output
with and without converting the input values.
 Write a JavaScript program to convert current date into string.
www.webstackacademy.com ‹#›www.webstackacademy.com
Regular Expression
(RegEx)
(JavaScript)
www.webstackacademy.com
Regular Expression
 Regular expressions are used for defining String patterns that can be used
for searching, manipulating and editing a text.
 A regular expression, regex or regexp, in theoretical computer science.
 The process of searching text to identify matches—strings that match a
regex's pattern—is pattern matching.
 In JavaScript, regular expressions are also objects.
 Regular expressions can be used to perform text search and text replace
operations.
www.webstackacademy.com
Regular Expression
 Pattern:
ļ‚§ Pattern specifications consist of a series of characters
ļ‚§ These characters have a special meaning
ļ‚§ They are also known as meta characters
Syntax:
var match = new RegExp(pattern, modifiers) (or)
var match = /pattern/modifiers
www.webstackacademy.com
Regular Expression
 Modifiers are a series of characters indicating various options
 They are optional in a regular expression
 This syntax is borrowed from Perl, supports some of them
 Perl was originally designed for pattern matching
Option Description
g Global matching. When using the replace() method, specify this modifier to replace
all matches, rather than only the first one.
i Case insensitive matching
m Multi-line mode. In this mode, the caret and dollar match before and after newlines
in the subject string
www.webstackacademy.com
Regular Expression
var pattern = /Fruit/i;
Option Description
/Fruit/i Regular Expression
Fruit Search pattern
i The search should be case insensitive
www.webstackacademy.com
Regular Expression patterns
Character Meaning
 Indicates that the next character is special and not to be interpreted
literally
^ Matches the beginning of the string or line.
$ Matches the end of the string or line.
* Matches the previous character 0 or more times.
+ Matches the previous character 1 or more times.
n Matches a New line
? Matches the previous character 0 or 1 time.
. Find a single character, except newline or line terminator
www.webstackacademy.com
Regular Expression Pattern
Brackets are used to find range of characters.
Expression Description
[abc] Find any of the characters between the bracket
[^abc] Find any character NOT between the brackets
[0-9] Find any of the digits between the brackets
[^0-9] Find any character NOT between the brackets (any non-digit)
(x|y) Find any of the alternatives separated with |
www.webstackacademy.com
Regular Expression Example
<script>
function upper_case(str)
{
var regexp = /^[A-Z]/;
if (regexp.test(str))
{
console.log("String's first character is uppercase");
}
else
{
console.log("String's first character is not uppercase");
}
}
upper_case('Webstack Academy');
upper_case('webstack academy');
</script>
www.webstackacademy.com
Metacharacters
Meta character Description
d Find a digit
D Find a non digit character
w Find a word character
W Find a non word character
s Find a whitespace character
S Find a non-whitespace character
b Find a match at the beginning or end of the word
B Find a match not at the beginning or end of the word
0 Find a Null character
uxxxx Find a uni-code character specified by the hexadecimal number xxxx
www.webstackacademy.com
Examples
var pattern =/^abc/;
var pattern = /xy{3,5}z/;
var pattern =/[a-z]/;
var pattern = /abc.d/;
Matches only those string beginning with abc.
Matches a single ā€œxā€ followed by y characters between three and five and then the
letter z
Matches any lowercase alphabetic character
Matches any character except a newline
www.webstackacademy.com
Exercise
 WAP that will match any string containing ā€œworldā€.
 WAP to check whether the given string is alphanumeric or not
 WAP to validate the mobile number in a given format (+91 98412 32534)
 WAP to validate credit card in a given format (4312-8763-9823-1234)
 WAP to validate a discount code (PIZDIS400)
 Invalid format
 Invalid coupon code
www.webstackacademy.com ‹#›www.webstackacademy.com
Regular Expression
Methods
(JavaScript)
www.webstackacademy.com
The Exec() Method
The exec() method of the RegExp object is used to execute the search for a
match in a specified string.
Example:
/c/.exec("In JavaScript, regular expressions are also objects.");
Syntax:
Regexp.exec(str);
www.webstackacademy.com
The test() Method
 The test() method of the RegExp executes a search for a match between a
regular expression and a specified string.
 It will return true or false.
Syntax:
Regexp.test(str);
www.webstackacademy.com
Example
<script>
regExpr = new RegExp('like','g')
// Define a string.
str1 = 'Happiness radiates like the fragrance from a flower';
// Check whether regular expression exists in the string.
if (regExpr.test(str1))
{
document.write("'like' is found in " + str1);
}
</script>
www.webstackacademy.com ‹#›www.webstackacademy.com
String Methods for
Regular Expression
(JavaScript)
www.webstackacademy.com
String Methods for Regular Expressions
It will return the index of character at which the first matching substrings begins.
<script>
var str = "I like apple";
var n = str.search(/Apple/i);
document.write(n);
</script>
Output : 7
Syntax:
str.search(pattern);
www.webstackacademy.com
The split()
• The split() method splits a string into sub strings and return them in an array
• Separator is used to split the string
• Limit specifies the number of splits
Syntax:
str.split(separator,limit);
<script>
var str = ā€œ10/3/5/6/5";
var split = str.split(/[/]+/);
document.write(split);
</script>
www.webstackacademy.com
The replace()
var str = "Hello.How are You?";
// Replace first dot with exclamation mark (!)
var res = str.replace(/./, "!");
alert(res);
Syntax:
str.search(searchvalue,newvalue);
• The search() method searches for the given String / RegExp, replaces new value
• In case of RegExp the first match is replaced. For all replacements use ā€˜g’ modifier
www.webstackacademy.com
WebStack Academy
#83, Farah Towers,
1st Floor, MG Road,
Bangalore – 560001
M: +91-809 555 7332
E: training@webstackacademy.com
WSA in Social Media:

More Related Content

PDF
Asp.net state management
priya Nithya
Ā 
PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
Ā 
PDF
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
Ā 
PDF
jQuery for beginners
Arulmurugan Rajaraman
Ā 
PDF
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Edureka!
Ā 
PDF
JavaScript Programming
Sehwan Noh
Ā 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
Ā 
PPTX
Java constructors
QUONTRASOLUTIONS
Ā 
Asp.net state management
priya Nithya
Ā 
Lab #2: Introduction to Javascript
Walid Ashraf
Ā 
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
Ā 
jQuery for beginners
Arulmurugan Rajaraman
Ā 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Edureka!
Ā 
JavaScript Programming
Sehwan Noh
Ā 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
Ā 
Java constructors
QUONTRASOLUTIONS
Ā 

What's hot (20)

PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
Ā 
PPT
Class 5 - PHP Strings
Ahmed Swilam
Ā 
PPT
Exception Handling in JAVA
SURIT DATTA
Ā 
PPTX
Nodejs functions & modules
monikadeshmane
Ā 
PPTX
Javascript functions
Alaref Abushaala
Ā 
PPTX
Javascript
Nagarajan
Ā 
PPSX
Javascript variables and datatypes
Varun C M
Ā 
PDF
Javascript basics
shreesenthil
Ā 
PPTX
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
Ā 
PDF
JavaScript: Variables and Functions
Jussi Pohjolainen
Ā 
PPTX
Event In JavaScript
ShahDhruv21
Ā 
PPTX
jQuery
Jay Poojara
Ā 
PPT
JQuery introduction
NexThoughts Technologies
Ā 
PPTX
jstl ( jsp standard tag library )
Adarsh Patel
Ā 
PDF
TypeScript - An Introduction
NexThoughts Technologies
Ā 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
Ā 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
Ā 
PPT
Java Script ppt
Priya Goyal
Ā 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
Ā 
Class 5 - PHP Strings
Ahmed Swilam
Ā 
Exception Handling in JAVA
SURIT DATTA
Ā 
Nodejs functions & modules
monikadeshmane
Ā 
Javascript functions
Alaref Abushaala
Ā 
Javascript
Nagarajan
Ā 
Javascript variables and datatypes
Varun C M
Ā 
Javascript basics
shreesenthil
Ā 
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
Ā 
JavaScript: Variables and Functions
Jussi Pohjolainen
Ā 
Event In JavaScript
ShahDhruv21
Ā 
jQuery
Jay Poojara
Ā 
JQuery introduction
NexThoughts Technologies
Ā 
jstl ( jsp standard tag library )
Adarsh Patel
Ā 
TypeScript - An Introduction
NexThoughts Technologies
Ā 
PHP FUNCTIONS
Zeeshan Ahmed
Ā 
ReactJS presentation.pptx
DivyanshGupta922023
Ā 
Java Script ppt
Priya Goyal
Ā 
Ad

Similar to JavaScript - Chapter 9 - TypeConversion and Regular Expressions (20)

PPT
Regular expressions
Raj Gupta
Ā 
PPT
9781305078444 ppt ch08
Terry Yoast
Ā 
DOCX
WD programs descriptions.docx
anjani pavan kumar
Ā 
PPTX
Ch08 - Manipulating Data in Strings and Arrays
dcomfort6819
Ā 
PPT
2.regular expressions
Praveen Gorantla
Ā 
PPTX
Javascript - Break statement, type conversion, regular expression
Shivam gupta
Ā 
PPTX
Regular expressions in Python
Sujith Kumar
Ā 
PPT
Java căn bản - Chapter9
Vince Vo
Ā 
PPTX
Regex1.1.pptx
VigneshK635628
Ā 
PPT
Chapter 9 - Characters and Strings
Eduardo Bergavera
Ā 
PPT
Form validation client side
Mudasir Syed
Ā 
PPTX
Java: Regular Expression
Masudul Haque
Ā 
PDF
What is JavaScript Regex | Regular Expressions in JavaScript | Edureka
Edureka!
Ā 
PPT
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
Ā 
PDF
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
Ā 
PPTX
Regular expressions in php programming language.pptx
NikhilVij6
Ā 
PDF
Regular Expressions: JavaScript And Beyond
Max Shirshin
Ā 
ODP
String interpolation
Knoldus Inc.
Ā 
PPTX
JavaScript.pptx
Govardhan Bhavani
Ā 
DOCX
Python - Regular Expressions
Mukesh Tekwani
Ā 
Regular expressions
Raj Gupta
Ā 
9781305078444 ppt ch08
Terry Yoast
Ā 
WD programs descriptions.docx
anjani pavan kumar
Ā 
Ch08 - Manipulating Data in Strings and Arrays
dcomfort6819
Ā 
2.regular expressions
Praveen Gorantla
Ā 
Javascript - Break statement, type conversion, regular expression
Shivam gupta
Ā 
Regular expressions in Python
Sujith Kumar
Ā 
Java căn bản - Chapter9
Vince Vo
Ā 
Regex1.1.pptx
VigneshK635628
Ā 
Chapter 9 - Characters and Strings
Eduardo Bergavera
Ā 
Form validation client side
Mudasir Syed
Ā 
Java: Regular Expression
Masudul Haque
Ā 
What is JavaScript Regex | Regular Expressions in JavaScript | Edureka
Edureka!
Ā 
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
Ā 
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
Ā 
Regular expressions in php programming language.pptx
NikhilVij6
Ā 
Regular Expressions: JavaScript And Beyond
Max Shirshin
Ā 
String interpolation
Knoldus Inc.
Ā 
JavaScript.pptx
Govardhan Bhavani
Ā 
Python - Regular Expressions
Mukesh Tekwani
Ā 
Ad

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
Ā 
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
Ā 
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
Ā 
PDF
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
Ā 
PDF
Webstack Academy - Internship Kick Off
WebStackAcademy
Ā 
PDF
Building Your Online Portfolio
WebStackAcademy
Ā 
PDF
Front-End Developer's Career Roadmap
WebStackAcademy
Ā 
PDF
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
Ā 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
Ā 
PDF
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
Ā 
PDF
Angular - Chapter 5 - Directives
WebStackAcademy
Ā 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
Ā 
PDF
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
Ā 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
Ā 
PDF
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
Ā 
PDF
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
Ā 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
Ā 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
Ā 
PDF
JavaScript - Chapter 11 - Events
WebStackAcademy
Ā 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
Ā 
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
Ā 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
Ā 
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
Ā 
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
Ā 
Webstack Academy - Internship Kick Off
WebStackAcademy
Ā 
Building Your Online Portfolio
WebStackAcademy
Ā 
Front-End Developer's Career Roadmap
WebStackAcademy
Ā 
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
Ā 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
Ā 
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
Ā 
Angular - Chapter 5 - Directives
WebStackAcademy
Ā 
Angular - Chapter 3 - Components
WebStackAcademy
Ā 
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
Ā 
Angular - Chapter 1 - Introduction
WebStackAcademy
Ā 
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
Ā 
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
Ā 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
Ā 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
Ā 
JavaScript - Chapter 11 - Events
WebStackAcademy
Ā 
JavaScript - Chapter 8 - Objects
WebStackAcademy
Ā 

Recently uploaded (20)

PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
Ā 
PDF
Doc9.....................................
SofiaCollazos
Ā 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
Ā 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
Ā 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
Ā 
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
Ā 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
Ā 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
Ā 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
Ā 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
Ā 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
Ā 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
Ā 
DOCX
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
Ā 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
Ā 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
Ā 
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
Ā 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
Ā 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
Ā 
PDF
Software Development Methodologies in 2025
KodekX
Ā 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
Ā 
Doc9.....................................
SofiaCollazos
Ā 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
Ā 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
Ā 
REPORT: Heating appliances market in Poland 2024
SPIUG
Ā 
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
Ā 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
Ā 
Presentation about Hardware and Software in Computer
snehamodhawadiya
Ā 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
Ā 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
Ā 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
Ā 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
Ā 
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
Ā 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
Ā 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
Ā 
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
Ā 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
Ā 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
Ā 
Software Development Methodologies in 2025
KodekX
Ā 

JavaScript - Chapter 9 - TypeConversion and Regular Expressions