There is a code:

if (e.name == vasya) { // выполняем что то } 

so let's say e.name is "Vasily" and so, and "vasya" equals "Vasily Petrovich", so in this code the function will work only if it ideally equals "Vasily" and how to make it so that it would work if the symbol is not perfect How is it with a small letter and also a last name?

  • 2
    regular expressions should be used - lexxl
  • give an example of code that would fit in this situation - arthru

2 answers 2

Option 1 (using indexOf):

 var name = 'Василий'; var value = 'василий петрович'; var _name = name.toLowerCase(); var _value = value.toLowerCase(); if (_value.indexOf(_name) != -1) { alert('Found!'); } 

Option 2 (using regular expressions):

 var name = 'Василий'; var value = 'василий петрович'; name = name.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); var needle = new RegExp(name, 'ig'); if (needle.test(value)) { alert('Found!'); } 
  • new RegExp(name, 'ig'); - only this was not enough. - Qwertiy
  • How would you write? - Robert Dampilon
  • First, why turn the name into a regular? Secondly, and who will escape characters? - Qwertiy
  • e.name is most likely a “dynamic” variable, and creating a regular expression using variables is possible only in this way. Concerning the screening I agree. I will correct - Robert Dampilon

Use the String.match () or String.indexOf () methods to search the string.

  • I tried something that I can't do, give an example of the code for this particular situation - arthru