10 – Java Script (3) Informatics Department Parahyangan Catholic University.
-
Upload
osborne-green -
Category
Documents
-
view
218 -
download
0
Transcript of 10 – Java Script (3) Informatics Department Parahyangan Catholic University.
Pemrograman Berbasis Web10 – Java Script (3)
Informatics DepartmentParahyangan Catholic University
Array’s Method (contd.)
Shifting Elements works like popping an element, but
instead of removing the last element, shift() method removes the first element and shifts all other elements forward.
works like dequeue in Queue data structure
Example:var fruits = ["Banana", "Orange", "Apple", "Mango"];var removed = fruits.shift(); //fruits = ["Orange", "Apple", "Mango"]//removed = “Banana”
Array’s Method (contd.)
Unshifting Elements The unshift() method adds a new
element at the beginning of an array and shifts the old elements backward
Example:var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.unshift("Lemon"); //fruits = ["Lemon", "Banana", "Orange", "Apple", "Mango"]
Array’s Method (contd.)
Deleting Elements Changes an element to undefined This leaves a “hole” in an array
Example:var fruits = ["Banana", "Orange", "Apple", "Mango"];delete fruits[1];//fruits = ["Banana", undefined, "Apple", "Mango"]
Array’s Method (contd.)
Splicing an Array The splice() method can be used to add
new items or remove some items to/from an array
Example:var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.splice(2, 1, "Lemon", "Kiwi");//fruits = ["Banana", "Orange", "Lemon", "Kiwi", "Mango"]
where to splice
how many elements should be removed
new elements to be inserted
Array’s Method (contd.)
Slicing an Array The slice() method slices out a piece of
an array into a new array
Example:var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];var citrus = fruits.slice(1, 3);//citrus = ["Orange", "Lemon“]
starting indexending index (not included in the result)
Array’s Method (contd.)
Searching in an Array The indexOf() method searches the
array for an element and return its position (first to be found)
The lastIndexOf() method searches the array for and element, starting at the end, and returns its position
Example:var foo = ["H", "E", "L", "L", "O"];var firstL = foo.indexOf("L"); //2var lastL = foo.lastIndexOf("L"); //3
Regular Expression
A regular expression is a sequence of characters that forms a search pattern.
The search pattern can be used for text search and text replace operations.
Regular Expression
Syntax: /pattern/modifiers;
Example:
w3school is a pattern (to be used in search) i is a modifier (modifies the search to be case-
insensitive)
var patt = /w3schools/i;
Regular Expression
In Java Script, RE usually used for search() and replace() string methods
Example:var str = "Visit W3Schools";var n = str.search(/w3schools/i); //n = 6
var str = "Visit W3Schools!";var n = str.search("W3Schools"); //n = 6
var str = "Visit W3Schools!";var n = str.search(“w3schools"); //n = -1
Regular Expression
Example:var str = "Visit Microsoft!";var res = str.replace(/microsoft/i, "W3Schools");//res = "Visit W3Schools!"var str = "Visit Microsoft!";var res = str.replace("Microsoft", "W3Schools");//res = "Visit W3Schools!"var str = "Visit Microsoft!";var res = str.replace(”microsoft", "W3Schools");//res = "Visit Microsoft!"
Regular Expression
Modifiers i
perform case-insensitive matching g
perform a global match (find all the matches rather than stopping after the first match)
mperform multiline matching (find first occurrences in every line)
Regular Expression
Patterns:Brackets are used to find a range of characters [abc]
find any characters which is a, b or c [^abc]
find any characters which is not a, not b, and not c
[a-z]find any characters between a and z (inclusive)
[^a-z]find any characters which is not a, b, ... , z (inclusive)
Regular Expression
Quantifiers: n+
matches any string that contains at least one n
n*matches any string that contains zero or more occurrences of n
n?matches any string that contains zero or one occurrences of n
Regular Expression
The test() method is a RE method. It searches a string for a pattern, and returns true or false, depending on the result.
Example:var patt = /free/;patt.test("The best things in life are free!");//returns true since the string has “free” in itvar patt = /frees/;patt.test("The best things in life are free!");//returns false since the string doesn’t has “frees” in it
Regular Expression
Example:var patt = /frees*/;patt.test("The best things in life are free!");//returns true since the string has “free” in it. Note that s* means zero s is permitable
var patt = /frees+/;patt.test("The best things in life are free!");//returns false since the string has “frees” in itvar patt = /fre?!/;patt.test("The best things in life are free!");//returns false since e?! means zero or one e then followed by a “!”. In this string there is 2 “e”s