ngplayground ngplayground - 2 months ago 20x
jQuery Question

jQuery detect if string contains something

I'm trying to write jQuery code to detect if a live string contains a specific set of characters then the string alerts me.


<textarea class="type"></textarea>

My Jquery

$('.type').keyup(function() {
var v = $('.type').val();
if ($('.type').is(":contains('> <')")){
console.log('contains > <');

if for example I typed the following

> <a href="">Google</a> <a href="">Yahoo</a>

My code should console log alert me that there > < present in the string.


You could use String.prototype.indexOf to accomplish that. Try something like this:

$('.type').keyup(function() {
  var val = this.value;
  if ( val.indexOf('> <') !== -1 ) {
    console.log('contains ', val);