jQuery检测字符串是否包含某些内容

ngp*_*und 18 jquery

我正在尝试编写jQuery代码来检测实时字符串是否包含一组特定的字符然后字符串提醒我.

HTML

<textarea class="type"></textarea>
Run Code Online (Sandbox Code Playgroud)

我的Jquery

$('.type').keyup(function() {
    var v = $('.type').val();
    if ($('.type').is(":contains('> <')")){
        console.log('contains > <');        
    }
    console.log($('.type').val());
});
Run Code Online (Sandbox Code Playgroud)

例如,我键入以下内容

> <a href="http://google.com">Google</a> <a href="http://yahoo.com">Yahoo</a>
Run Code Online (Sandbox Code Playgroud)

我的代码应该控制日志警告我那里> <字符串中的当前.

yck*_*art 34

你可以String.prototype.indexOf用来完成它.尝试这样的事情:

$('.type').keyup(function() {
  var v = $(this).val();
  if (v.indexOf('> <') !== -1) {
    console.log('contains > <');
  }
  console.log(v);
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="type"></textarea>
Run Code Online (Sandbox Code Playgroud)


Den*_*ret 7

你得到textarea的值,使用它:

$('.type').keyup(function() {
    var v = $('.type').val(); // you'd better use this.value here
    if (v.indexOf('> <')!=-1) {
       console.log('contains > <');        
    }
});
Run Code Online (Sandbox Code Playgroud)


top*_*at3 5

你可以使用javascript的indexOf函数.

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";
if(str1.indexOf(str2) != -1){
   alert(str2 + " found");
}
Run Code Online (Sandbox Code Playgroud)