• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JScript » String » JavaScript String search() Method
Next →
← Prev

JavaScript String search() Method

By Dinesh Thakur

JavaScript String search() Method: Whenever working with a strings, sometimes we need to determine if a string contains some specific substring, and if it does, you need to determine where in the string that substring occurs.

The JavaScript search() Method executes the search for a match between a regular expression and a specified string. If a match is found, it returns the position in the string where the beginning of the match was found. Otherwise, it returns –1.

Syntax for JavaScript String search() Method is:

string.search(regexp) 

For instance, if you have the string “I am Ironman!” and you search for the substring “Iron”, you want to know that the string contains “Iron” but also where “Iron” occurs. You can perform this type of search with the JavaScript search() Method of the string object.

To perform this search is simple. If “I am Ironman!” is stored in the variable mystring, you would search for “Iron” with the JavaScript search() Method:

mystring.search("Iron"); 

JavaScript search() Method returns a numeric value indicating the position in the string where it found “is”. In this case, that position is 5.

The following task searches for a substring in another string stored in a variable and displays the position where that substring is found:

1. Open a new HTML document in your preferred HTML or text editor.

2. Create the body of the document with opening and closing body tags:

<body> 
</body> 

3. Insert a script block in the body of the document:

<script language="JavaScript"> 
   <!--// 
  --> 
</script> 

4. Create a variable named mystring and assign the value “I am Ironman!” to it:

var mystring = "I am Ironman!"; 

Display the results of the JavaScript search() Method so that the final page looks like

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
   <head>  
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />  
    <title>JavaScript Search() method </title>  
   </head>  
<body> 
 <h1 style="color: blue"> JavaScript search() Method</h1>  <hr />  
<script type="text/javascript">  
    var mystring = "I am Ironman!";  
    var tomatch = /Iron/;  
      if (mystring.search(tomatch)) {  
       window.alert("Iron found at position "+mystring.search(tomatch)+"!");  }  
     else {  window.alert("Sorry, no Iron in your string.");  }  
</script>  
</body>  
</html> 

Save the file and close it.

5. Open the file in a browser. You should see the number 5 displayed in the browser as in Figure

JavaScript search() Method

You’ll also like:

  1. JavaScript String substring() Method
  2. JavaScript String substr() Method
  3. JavaScript String split() Method
  4. JavaScript String match() Method
  5. JavaScript String replace() Method
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

JavaScript Tutorials

JavaScript Tutorials

  • JavaScript - Home
  • JavaScript - Operators
  • JavaScript - Comments
  • JavaScript - If...Else
  • JavaScript - For Loop
  • JavaScript - Popup Boxes
  • JavaScript - Switch Statement
  • JavaScript - while Loop
  • JavaScript - Functions
  • JavaScript - Variables
  • JavaScript - Statements
  • JavaScript - Substring()
  • JavaScript - indexOf()
  • JavaScript - Match()
  • JavaScript - Replace()
  • JavaScript - Search()
  • JavaScript - Split()
  • JavaScript - Substr()
  • JavaScript - Blink()
  • JavaScript - Anchor()
  • JavaScript - Big()
  • JavaScript - Strings Object
  • JavaScript - String Format
  • JavaScript - Length Property
  • JavaScript - Prototype
  • JavaScript - Slice Substr Vs Substring
  • JavaScript - JavaScript:void(0)

Other Links

  • JavaScript - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW