-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13 string search.js
More file actions
119 lines (87 loc) · 3.2 KB
/
13 string search.js
File metadata and controls
119 lines (87 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ****************JavaScript String Search************
JavaScript methods for searching strings:
String.indexOf()
String.lastindexOf()
String.startsWith()
String.endsWith()
*/
/*
*************String.indexOf()***************
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:
*/
var text="RANA ABOBAKAR SAB KYA HAL HA.";
var s=text.indexOf('RANA');
console.log(s);//0
/*
String.lastIndexOf()
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
*/
var text="RANA ABOBAKAR SAB KYA HAL HA RANA";
var s=text.lastIndexOf('RANA');
console.log(s);//29
//Both indexOf(), and lastIndexOf() return -1 if the text is not found:
var text="RANA ABOBAKAR SAB KYA HAL HA RANA";
var s=text.lastIndexOf('RNA');
console.log(s);//-1
//Both methods accept a second parameter as the starting position for the search:
var text="RANA ABOBAKAR SAB KYA HAL HA RANA";
var s=text.lastIndexOf('RANA',10);
console.log(s);//0
/*
The lastIndexOf() methods searches backwards (from the end to the beginning),
meaning: if the second parameter is 10, the search starts at position 10,
and searches to the beginning of the string.
*/
/*
***********************String.search()*******************
The search() method searches a string for a specified value and returns the position of the match:
*/
var text="RANA ABOBAKAR SAB KYA HAL HA RANA";
var s=text.search("ABOBAKAR");
console.log(s);//5
/*
Did You Notice?
The two methods, indexOf() and search(), are equal?
They accept the same arguments (parameters), and return the same value?
The two methods are NOT equal. These are the differences:
The search() method cannot take a second start position argument.
The indexOf() method cannot take powerful search values (regular expressions).
You will learn more about regular expressions in a later chapter.
*/
/*
******************String.match()*****************
The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
*/
var text="RANA ABOBAKAR SAB KYA HAL HA RANA";
s=text.match(/RANA/g);
console.log(s);//[ 'RANA', 'RANA' ]
// If the regular expression does not include the g modifier (to perform a global search),
// the match() method will return only the first match in the string.
s=text.match(/rana/ig);
console.log(s);//[ 'RANA', 'RANA' ]
/*
String.includes()
The includes() method returns true if a string contains a specified value
string.includes(searchvalue, start)
*/
text="RANA ABOBAKAR SAB KYA HAL HA RANA";
s=text.includes("RANA");
console.log(s);//true
/*
String.startsWith()
string.startsWith(searchvalue, start)
The startsWith() method returns true if a string begins with a specified value, otherwise false:
*/
text="RANA ABOBAKAR SAB KYA HAL HA RANA";
s=text.startsWith("ABOBAKAR");
console.log(s);//false
/*
String.endsWith()
The endsWith() method returns true if a string ends with a specified value, otherwise false:
string.endswith(searchvalue, length)
searchvalue Required. The value to search for.
length Optional. The length to search.
*/
text="RANA ABOBAKAR SAB KYA HAL HA RANA";
s=text.endsWith("ABOBAKAR");
console.log(s);//false