selected radio button value in Javascript

In this blog, we know the difference between search() and indexOf() in javascript. We also know which one is faster and reliable to use.
First, we discuss

Search() Method

Description

The search() method is used to search for a match between a regular expression and a specified string. If a match is found, search() method returns the index of the regular expression inside the string, otherwise, it returns -1.

Syntax

Its syntax is as follows −

string.search(regexp);

Argument Details

regexp − A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

Example

<html> 
<head> 
<title>JavaScript String search() Method</title> 
</head> 
<body> 
<script type = "text/javascript"> 
var str = "friends say Hello"; 
var substr = "Hello"; 
var index = str.search(substr); 
if(index!=-1) { 
document.write(substr + " found at " + index + " position.<br>"); 
} 
else { 
document.write(substr + " does not exist in the " + str + ".<br>"); 
} 
</script> 
</body> 
</html>

Output

Hello found at 12 position.

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

You May Also Like:

IndexOf() Method

Description

The indexOf() method returns the index within the calling string object of the first occurrence of the specified value, starting the search at startIndex. It returns -1 if the value is not found.

Syntax

Use the following syntax to use the indexOf() method.

string.indexOf(searchValue[, fromIndex])

Argument Details

  • searchValue − A string representing the value to search for.
  • fromIndex − The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.

Example

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title>JavaScript String IndexOf() Method</title> 
</head> 
<body> 
<script type="text/javascript"> 
var Str="Brave new world"; 
document.write("The index of the first w from the beginning is " + Str.indexOf("w")+"<br />"); 
document.write("The index of 'new' from the beginning is " + Str.indexOf("new")); 
</script> 
</body> 
</html>

Output

The index of the first w from the beginning is 8
The index of 'new' from the beginning is 6

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

Though indexOf() and search() both methods are used to check whether a substring exists in the string or not and returns either of the index of the substring or -1 (if substring does not exist in the string), there is a difference between indexOf() and search() methods in javascript.

Now, you can clearly see that in indexOf() method, there is an optional parameter(offset) from where we can start the searching but the method search() does not have this feature. It simply takes the substring and starts searching from the 0th index.

Thanks for Reading, Keep Visiting.

0 Comments

No Comment.