JavaScript has rich library of functions to work with array and string. Sometimes working in Javascript, you might need to verify if a string contains specific substring or not.
This is same method as we use %Like%
query with SQL. There are two ways, you can check if string contains substring.
String.prototype.includes()
returns true if string contains any substring or not. For example,
var mainString = "laravelcode.com";
var subString = "stuff";
console.log(mainString.includes(subString)); // true
String.prototype.includes was introduced in ECMAScript 6, which not support to Internet Explorer or older browsers.
String.prototype.indexOf
supports ECMAScript 5 or older, so it will return -1 if string not contains substring. For example,
var mainString = "laravelcode.com";
var subString = "stuff";
console.log(mainString.indexOf(subString) !== -1); // true
I hope you liked this article and it will help a little on your project.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to Get the data-id Attribute of an Element Using jQuery
Use the jQuery attr() Method You can...How to configure NFS on Linux
NFS or Network File System is used to sh...jQuery text method
jQuery text() method allows you to get t...Laravel 8 get all Files in Directory Example
You may want to get images from server a...How to get and set Value in Form Fields in Javascript
In this article, we will discuss on sett...