It is five ways to determine if a string contains another string in JS.

Method 1: IndexOf () (recommended)

 VAR Str = "123" Console.log (Str.IndexOf ("2")! = -1); // true   
indexof () method can return a specified The string value of the string value first appears in the string. If the string value to be retrieved does not appear, the method returns -1.

Method 2: MATCH ()

VAR STR = “123” VAR REG = regexp (/ 3 /); if (str.match (reg)) {// contains;}

Match () method can retrieve the specified value in the string, or find one or Matching multiple regular expressions.
  Method 3: Search ()  
VAR STR = “123” Console.log (Str.Search “2”)! = -1); // True

Search () method is used to retrieve the sub-string specified in the string, or retrieved and regular expressions Match the sub-string. Returns -1 if any matching substrings are not found.

Method 4: Test ()

   VAR Str = "123" var REG = RegeXP (/ 3 /); console.log (reg.test (str)! = -1); // true 

Test () method is used Retrieve the value specified in the string. Returns True or False.

Method 5: Exec ()

VAR STR = “123” Var REG = Regexp (/ 3 / ); if (REG.EXEC (STR)) {// includes;}

EXEC () method
for retrieving regular in strings Expression matching. Returns an array in which the matching results are stored. If matching is not found, the return value is NULL.
 If you want to learn from JS in-depth system, you can refer to JavaScript core technology to develop decryption this classic reading.   Summary 
The above is the five ways to determine if a string contains another string in JS, and I hope to everyone. Help, if you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment