JavaScript String search()

📔 : Java Script 🔗

JavaScript में search() method , दी गयी value को String में search करता है। match होने पर उस value की position return करता है। search की जाने वाली value कोई भी String / SubString या regular expression हो सकता है।

JavaScript String search Syntax

string.search(search_value);
Parameters
  1. string | required : यह String variable है जिसमे हमें value को search करना है।

  2. search_value | required : value जो String में search करनी है।

  3. Return Value : value match होने पर उस value की position return केता है , और match न होने पर -1 return करता है।

JavaScript String search Example

File : js_string_search.html

CopyFullscreenClose FullscreenRun
<!DOCTYPE html>
<html>
  <body>
    <script>
      let str = "Visit learnhindituts.com to learn PHP , JavaScript , jQuery etc.";
      document.writeln(str.search("PHP"));
      document.writeln("<br>");
      /*these method searches value case sensitive*/
      document.writeln(str.search('php'));
    </script>
  </body>
</html>
Output
34
-1 

Note : अगर search() method में कोई value pass नहीं करते हैं तो 0 return होता है। और अगर किसी Empty String में कुछ search करने पर -1 return होता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook