Skip to content

Buzzingz

Unleashing the Power of Code

  • HTML
  • CSS
  • JavaScript
  • Toggle search form

JavaScript String Search

Posted on April 2, 2023April 2, 2023 By shani No Comments on JavaScript String Search

In JavaScript, you can search for a specific substring within a string using the search() method. The search() method returns the index of the first occurrence of the specified substring, or -1 if the substring is not found.

Example

string.search(searchvalue)

The searchvalue parameter is the substring you want to search for within the string.

example

var str = "Hello world!";
var n = str.search("world");

In this example, the search() method is used to search for the substring "world" within the string "Hello world!". The variable n will contain the value 6, which is the index of the first occurrence of the substring.

If the specified substring is not found within the string, the search() method will return -1.

var str = "Hello world!";
var n = str.search("foo");

In this example, the search() method is used to search for the substring "foo" within the string "Hello world!". Since the substring is not found within the string, the variable n will contain the value -1.

It’s important to note that the search() method is case-sensitive. If you want to perform a case-insensitive search, you can convert both the string and the substring to lowercase or uppercase before performing the search.

var str = "Hello world!";
var n = str.toLowerCase().search("hello");

In this example, the toLowerCase() method is used to convert the string "Hello world!" to all lowercase letters before performing the search for the substring "hello". Since the search() method is case-sensitive, this will return the correct index of the first occurrence of the substring.

Javascript

Post navigation

Previous Post: JavaScript Strings
Next Post: JavaScript String Methods

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JavaScript Tutorial

  • JavaScript Tutorial
  • JavaScript Where To
  • JavaScript Output
  • JavaScript Statements
  • JavaScript Syntax
  • JavaScript Comments
  • JavaScript Variables
  • JavaScript Let
  • JavaScript Const
  • JavaScript Operators
  • JavaScript Arithmetic
  • JavaScript Assignment
  • JavaScript Data Types
  • JavaScript Functions
  • JavaScript Objects
  • JavaScript Events
  • JavaScript Strings
  • JavaScript String Methods
  • JavaScript String Search
  • JavaScript Template Literals
  • JavaScript Numbers
  • JavaScript BigInt
  • JavaScript Number Methods
  • JavaScript Number Properties
  • JavaScript Arrays
  • JavaScript Array Methods
  • JavaScript Sorting Arrays
  • JavaScript Array Iteration
  • JavaScript Array Const
  • JavaScript Date Objects
  • JavaScript Date Formats
  • JavaScript Get Date Methods
  • JavaScript Set Date Methods
  • JavaScript Math Object
  • JavaScript Random
  • JavaScript Booleans
  • JavaScript Comparison and Logical Operators
  • JavaScript if, else, and else if
  • JavaScript Switch Statement
  • JavaScript For Loop
  • JavaScript For In
  • JavaScript For Of
  • JavaScript While Loop
  • JavaScript Break and Continue
  • JavaScript Iterables
  • JavaScript Sets
  • JavaScript Maps
  • JavaScript typeof
  • JavaScript Type Conversion
  • JavaScript Bitwise Operations
  • JavaScript Regular Expressions
  • JavaScript Operator Precedence
  • JavaScript Errors
  • JavaScript Scope
  • JavaScript Hoisting: Understanding the Basics
  • JavaScript Use Strict: The Importance of Strict Mode in JavaScript
  • The JavaScript this Keyword
  • JavaScript Arrow Function: A Concise and Comprehensive Guide
  • JavaScript Classes: Understanding the Basics
  • JavaScript Modules
  • JavaScript JSON
  • JavaScript Debugging
  • JavaScript Style Guide
  • JavaScript Best Practices
  • JavaScript Common Mistakes: How to Avoid Them
  • JavaScript Performance: Techniques for Improving Your Code
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions

Copyright © 2023 Buzzingz.

Powered by PressBook WordPress theme