I can not figure out how to check for the presence of a character in the text using jQuery. For example, I have a var slovo = "88+23=" . What function can you check that the text has a "+" symbol and that would return true or false . Thought that $.search() would help, but something didn’t work.

  • one
    str.indexOf ('+') - returns a value other than -1 if in the string - Jean-Claude
  • why do you need jQuery? - korytoff

3 answers 3

indexOf to help:

 var slovo = "88+23="; if(slovo.indexOf('+') > 0 ){ console.log('есть'); } else { console.log('нет'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

  • indexOf - not the jquery method - Jean-Claude
  • And therefore it cannot be used in jQuery ??? !!! - HamSter
  • you drive, Elena)) you do not understand the basics. You have connected the library, which is not necessary, since the indexOf function is present in the native js. And how can a function be used in a library? A library is a set of functions, that is, indexOf can be used in a script along with other functions. Oh well, what am I talking about ....))))) ahaha. - Jean-Claude
  • I just understand it. But you are confused in your comments. - HamSter

You can use the test :

 var urVar = "22+55="; alert(/^.*\+.*$/.test(urVar)) 

Jsfiddle

  • there is a plus check, not a mask ... - Jean-Claude
  • I showed how to do it with the help of the regular one, but it’s not difficult to /^.*\+.*$/ .test (urVar) - bsbak

Can so

 $(function() { $(document).ready(function(){ var res = $('.str').text().indexOf('+')+1; if(res!=-1){ alert('Есть символ. Позиция символа - '+res); } else { alert('Cимвола нет'); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="str">88+22=</div>