method matches a string against a regular expression ** The search(>
method returns the index (position>
of the first match. The search(>
method returns - 1 assuming no match is found. The search(>
method is case sensitive.
JavaScript Search Mehods
String indexOf(>
The indexOf(>
method returns the index of (the position of>
the first occurence of a specified text in a string.
String lastIndexOf(>
The lastIndexOf(>
method returns the index of the last occurrence of a specified text in a string
Both above methods returns -1 if text not found
String startsWith(>
The startsWith(>
technique decides if a string starts with the characters of a specified string, returning true or false.
String endsWith(>
The endsWith(>
technique decides if a string ends with the characters of a specified string, returning true or false.
String search(>
The search(>
method searches a string for a specified value and returns the position of the match.
String match(>
The match(>
method searches a string for a match against a regular expression, and returns the matches, as an Array object.
String includes(>
The includes(>
method returns true if a string contains a specified value.
Let's see an example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String search methods</h2>
<p id="demo"></p>
<p id="demo1"></p>
<script>
let text = "Hello world";
document.write("Text =">
;
document.write(text>
;
let result = text.endsWith("world">
;
document.getElementById("demo">
.innerHTML = result;
let text1 = "document verfied";
document.write("\n\r">
;
document.write("Text1=">
;
document.write(text1>
;
let s = text1.startsWith("ver">
;
document.getElementById("demo1">
.innerHTML = s;
var str="JavaScript is a scripting language. Scripting languages are often interpreted";
document.write("\n">
;
document.writeln(str.search(/Scripting/>
>
;
</script>
</body>
</html>
Output
Try it here
We can help you solve company communication.