Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

JavaScript Search

by suresh

The JavaScript Search method is used to search and return the index position of the first occurrence of a specified string in a regular expression search. JavaScript Search will return -1 if the defined string not found. The syntax of the JavaScript Search function is

String_Object.Search(RegExp)

JavaScript Search Example

The following set of examples will help you understand the JS Search Function.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Search </title>
</head>
<body>
    <h1> JavaScript Search </h1>
<script>
  var Str1 = "Learn JavaScript at Tutorial Gateway.org";
  var Str2 = "We are ABC working at abc company";
 
  var Str3 = Str1.search("Script");
  var Str4 = Str1.search("abc"); // Non existing item
  var Str5 = Str2.search("abc");
  var Str6 = Str2.search(/abc/i);

  document.write("<b> Index position of Script is:</b> " + Str3);
  document.write("<br \> <b> Index position of abc is:</b> " + Str4);
  document.write("<br \> <b> Index position of abc is:</b> " + Str5);
  document.write("<br \> <b> Index position of abc is:</b> " + Str6);
</script>
</body>
</html>

OUTPUT

JavaScript Search

ANALYSIS

The following statement search for the index position of a substring “Script” and store the index value in Str3.

var Str3 = Str1.search("Script");

NOTE: You should count the space as One Character.

Here, we are searching for a non-existing string “abc” inside the Str1. Since Search function doesn’t find the substring, it is returning -1 as output

 var Str4 = Str1.search("abc"); // Non existing item

In the next line we are looking for “abc” inside the Str2.

 var Str5 = Str2.search("abc");

From the above statement you can observe that, although term abc is repeated multiple times, JS Search function consider the string “ABC” is different from “abc”. Now, let us use the i regular expression along with string

var Str6 = Str2.search(/abc/i);

Above statement performs the case insensitive string search So, Search function returns the index position of first occurrence i.e., ABC.

Placed Under: JavaScript

Trending Posts

Java Program to Copy an Array

Tableau Set

Clauses in SQL Server

JavaScript Match

MySQL UTC_TIMESTAMP

Java Program to Sort Array in Descending Order

Data Labels in Tableau Reports

SQL TRY CAST

MDX Head Function

How to Create Pareto Chart in Tableau

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy