|
| JAVASCRIPT search() METHOD
The search() method searches a string for specified character/s. Returns the position if found or -1 if not found.
The search() method takes one compulsory argument: the string or character tosearchfor. (Always remember that the first position of a string always starts at 0 and not 1).
The general format is:
string.search(/tosearchfor/);
Please note:
The tosearchfor argument must be enclosed in two forward slashes like: /tosearchfor/
By default, the search() method is case sensitive. To make it case insensitive place an i after the second forward slash like:
string.search(/tosearchfor/i);
Example: Default Search - case sensitive
<script type="text/javascript">
var string="The brown box is brown";
document.write(string.search(/brown/));
</script>
The above will output:
Example: Case Insensitive
<script type="text/javascript">
var string="The Brown box is brown";
document.write(string.search(/brown/i));
</script>
The above will output:
See Also:
|
|