How to use JavaScript search box?

img
JavaScript delivers several means that explore strings: indexOf, lastIndexOf, search, and the regular countenance method test.
These simple methods tell you either a match has been established or not.

The top 3 reappearance the location in the string of the match, or -1 if no match is found.
The test technique proceeds true or false. The string match technique in addition to the regular expression exec method deliver more particulars about search fallouts.
These are deliberated here.

Explanation and Procedure

This method is very easy because it will accept the single argument as well.
A design to search for in the string it is appealed on.
This argument could be a steady expression or else a string, which will routinely be changed to a consistent expression.

The search method is comparable to the indexOfand lastIndexOf approaches in that it yields the site of the first originated match, or -1 if it is not found.
We prove with a case- impervious search by means of the same string as displayed in examples below.

The search () method hunts a string for a stated value, and yields the position of the contest. The hunt value can be sequence or else a regular manifestation. This method will return -1 if no equal is originated.

Syntax of JavaScript search box

Now, we are going to share the syntax of JavaScript search box with you.
It is a very simple syntax.
You can use it again and again in your code and make your website more efficient.

string.search (searchvalue)

Parameter Values

The search value is a parameter of JavaScript search box.
It is a string which will be converted into a regular expression easily.
Required is also considered as a regular expression.
The technical details included in this code are return value in addition to JavaScript version.
The return value is just a number, which is used to represent the position of the specified searchvalue, or -1 if no match is found.
On the other hand, the JavScript version has a technical detail which is ECMA script 1.

More Examples

Example
Perform a case-sensitive search:

var str = "Mr. white has a white house";
var n = str.search("white");

Example
Perform a case-insensitive search:

var str = "Mr. white has a white house";
var n = str.search(/white /i);

JavaScript's String search Method

var str = 'A string to test JavaScript string methods';
var re = /String/i; // i flag for case-insensitive
var pos = str.search(re); // 2

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?