Symbol.search JavaScript

The JavaScript Symbol.search property finds out the index within a string that matches with the regular expression.

Syntax:

[Symbol.search](string) 

Parameters:
string: It represents the string that matches against the regular expression.

Returns:
Index of the specified string with match.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>    
class obj {  
constructor(a) {  
this.a = a;  }  
[Symbol.search](string) {   
return string.indexOf(this.a); }  }  
document.write('HELLO'.search(new obj('O')));   
</script>
</body>
</html>

Output:

4

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>  
class Searchtest {
  constructor(value) {
    this.value = value;
  }
  [Symbol.search](string) {
    return string.indexOf(this.value);
  }
}
 
document.write('w3spoint'.search(new Searchtest('java')));
 
</script>
</body>
</html>

Output:

5
Please Share