TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript String search() Method

JavaScript string search() Method with example, by string literal, by string object (using new keyword), javascript string methods, charAt(index) method, concat(str) method, indexOf(str) method, lastIndexOf(str) method, lowerCase() method, upperCase() method, slice(beginIndex, endIndex) method, trim() method, toLowerCase(), slice(), concat(), charCodeAt() method etc.

<< Back to JAVASCRIPT

JavaScript String search() Method

The JavaScript string search() method is used to search the regular expression in the given string. This method returns -1, if match is not found.

Syntax

The search() method is represented by the following syntax:

string.search(regexp)

Parameter

regexp - It represents the regular expression which is to be searched.

Return

The position of searched character.

JavaScript String search() Method Example

Let's see some examples of search() method.

Example 1

Let's see a simple example to search for a string.

<script>
var str="JavaScript is a scripting language. Scripting languages are often interpreted";
document.writeln(str.search("scripting"));
</script>
Test it Now

Output:

16

Example 2

In this example, we will see that search() method is case-sensitive.

<script>
var str="JavaScript is a scripting language. Scripting languages are often interpreted";
document.writeln(str.search(/Scripting/)); 
</script>
Test it Now

Output:

36

Example 3

We can ignore case-sensitive behaviour of search() method by using ignore flag. Let's understand with the help of example:

<script>
var str="JavaScript is a scripting language. Scripting languages are often interpreted";
document.writeln(str.search(/Scripting/i));
</script>
Test it Now

Output:

16

Example 4

In this example, we will search a regular expression which is not present in the given string.

<script>
var str="JavaScript is a scripting language. Scripting languages are often interpreted";
document.writeln(str.search(/TheDeveloperBlog/));
</script>
Test it Now

Output:

-1

Next TopicJavaScript String




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf