If I have a number of characters (either as a string or an array), does JavaScript have a function to tell me if any of those characters appear within a string?
For example, given the following, I want a function that returns true because str
does contain one of the characters in chars
.
const chars = [ '[', ']', '{', '}' ];
var str = "BlahBlah}";
In C#, I can use IndexOfAny()
, but I’m not finding a similar function in JavaScript.
4
Answers
Try this
JS contains metod includes. For example:
ES6 automates looping through arrays with
Array.prototype.some()
.Try:
Or, to prototype this to String yourself (not recommend):
Reusable Functional Approach