Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Last active January 27, 2021 08:21
Show Gist options
  • Save tangoabcdelta/8c34faf04b37ba30db6e41d696ed257c to your computer and use it in GitHub Desktop.
Save tangoabcdelta/8c34faf04b37ba30db6e41d696ed257c to your computer and use it in GitHub Desktop.
How to check if a string contains another substring in JavaScript?

All possibilities

new RegExp('word')).test(str) // ES2
str.indexOf('word') !== -1 // ES2
str.includes('word') // ES6
str.match(/word/gi)?.length > 0 //ES2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment