How to check if a string contains a certain substring?

JavaScript: How to check if a string contains a certain substring?

If you have a string foobar, you can check if it contains the string bar by doing this:

  1. let some_string = "foobar";
  2.  
  3. if (some_string.includes("foo")) {
  4.                 alert("contains it!") ;
  5. }

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to check if a string contains a certain substring?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the JavaScript language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

What does 'final class' and 'final function' (final method) mean in PHP?

How to find an item by Primary Key in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

What is snake case?

How to block access to certain referrers in Apache using .htaccess

What is the spaceship operator (<=>) in PHP?

How to add the CSRF (Cross-site request forgery) token in Laravel?

How to block an IP range based on CIDR in Apache with .htaccess

How to add the default charset to UTF-8 in .htaccess

How to convert a string to an array of it's characters in PHP?

What are the SPL data structures