Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Determine if a string contains any of an array of characters

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this

const chars = [ '[', ']', '{', '}' ];
const str = "BlahBlah}";

const result = chars.some(x=>str.includes(x))

console.log(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading