Animations
API
Arrays
Async
Basics
Challenges
Classes
Console
Dates
Debugging
DOM Elements
DOM Methods
DOM Properties
Event Listeners
Flow Control
Forms
Functions
Global Functions
JSON
Keywords
Libraries (3rd party)
Math
Modules
Objects
Snippets
String
Types
Widgets
Window Object
The search()
method searches a string for a specified value and returns the position (index) of the match. If it is NOT
found, it will return a -1.
// example 1
let str = "Please locate where 'locate' occurs!";
let pos = str.search("locate");
document.getElementById("demo1").innerHTML = pos;
The two methods, indexOf()
and search()
, pretty much do the same thing. are equal.
They accept the same arguments (parameters), and they return the same value.
The search()
method how ever can use RegEx expressions as parameters which makes it powerful.
// example 2
let str2 = "I like punch and pie.";
let position = str2.search(/pie/i);
let demo2 = document.getElementById("demo2");
demo2.innerHTML = position;
Regular Expression Modifiers
Modifiers can be used to perform case-insensitive searches.
i
- Perform case-insensitive matchingg
- Perform a global match (find all matches rather than stopping after the first match)m
- Perform multiline matching