I need to determine if string B is in string A for a certain number of characters. In my case, these are 6 characters:

For example,

строка А: 3897856787 строка B: 7856787 

If string B is in string A, it should return true; otherwise, false.

If a,

 строка А: 3897856787 строка B: 87 

In this case, it should return false, since the entry of only 2 characters.

I tried the includes method from lodash, but it returns true, even when 2 digits are entered.

Please tell me the solution!

  • And the option to check the string B through if does not fit? - Vitaliy Shebanits
  • one
    write the question in more detail ... I got the impression that it can be solved by a simple str.indexOf("подстрока") - Vitaly Shebanits

1 answer 1

 const isMatch = (str, match) => (( str.length > 5 ) ? match.indexOf(str) > -1 : false); const match = '123456789'; // Строка A const a = '123'; // Строка B const b = '345678'; // Строка C console.log(isMatch(a, match)) // false Строка A не содержит строку B console.log(isMatch(b, match)) // true Строка A содержит строку С // Кастом для внятности console.log(isMatch('112233', '0011223344')) // true 
  • Tell me, how in this function, check the last 5 digits for the occurrence? - drevival pm