js to determine if a string contains a certain character

Method 1 (recommended): indexOf()

indexOf() method: Returns the first occurrence of a specified string value in the string. If the string value to be retrieved does not appear, the method returns -1.

 var str = "LiHeErNAN" ; console . log ( str . indexOf ( "A" ) != - 1 ) ; // true

Method 2: match()

match() method: Retrieves a specified value within a string, or finds a match for one or more regular expressions.

 var str = "abcd" ; var reg = RegExp (

The post js Determining if a String Contains a Character first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/9071
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment