user4583167
user4583167

Reputation:

Jquery search for a word in a string

Hi so i am trying the following: I want to search for keywords in my text Area

My code works but now i would like to make it search for more than one word. So if my keyword is 3 words it must look for the 3 word in the text Area i hve no idea how to do this.

so if my keyword is "Mac Air" then it must look for the word "Mac Air" in the textarea

my code:

<div class="form-group">
<label><span class="error error-fields"></span>Focus Keyword</label>
<input type="text" class="form-control" id="keyword" name="keyword" placeholder="Focus Keyword">
</div>

<div class="form-group">
<label><span class="error-description error-fields"><?php echo $description_error ?></span>Page content</label>
<textarea rows="15" type="text" class="form-control" id="description" name="description" placeholder="Page Description"></textarea>  
<p><span class="contentcount"><b>0</b></span> words</p>
</div> 


    $('#description').keyup(function(e){ 

    var v = $(this).val().toLowerCase(),
        w = v.split(/\s/),
        needle = $('#keyword').val().toLowerCase(),
        c = 0;
    for (var i=0,len=w.length;i<len;i++){
        if (w[i] === needle){
            c++;
        }

        if(c > 0 && $('#keyword').val() != ''){
            var value = 'Yes';
            document.getElementById("seocontentshow").style.color = 'limegreen';
        }else{
            var value = 'No';
            document.getElementById("seocontentshow").style.color = 'red';
        } 

        checkstatus();  
        extrafunctions();
    }
    $('.content-content').text(value);
    });

Upvotes: 0

Views: 2608

Answers (3)

Ali Yousuf
Ali Yousuf

Reputation: 702

I think RegEx is too complicated for this thing. Just use indexOf():

$('#description').keyup(function(){ 
  var v = $(this).val().toLowerCase();
  var needle = $('#keyword').val().toLowerCase();
  var found = v.indexOf(needle) > -1;

  if (found) {
    value = 'Yes';
    document.getElementById("seocontentshow").style.color = 'limegreen';
  }else{
    value = 'No';
    document.getElementById("seocontentshow").style.color = 'red';
  } 

  checkstatus();  
  extrafunctions();
  $('.content-content').text(value);
});

Upvotes: 2

Chris Jaquez
Chris Jaquez

Reputation: 669

Regular expressions make the work really easy here.

$('#description').keyup(function() {
        var keyword = $('#keyword').val();
    var text = $('p').text();
    var pattern = new RegExp(keyword, 'gi');
    if(text.match(pattern))
        alert("Keyword found!");
    else
      alert("Keyword wasn't found");
});

Fiddle: https://jsfiddle.net/c2dxu3p6/

You can read on regular expressions here: https://en.wikipedia.org/wiki/Regular_expression

Also, here's a demo: https://regex101.com/r/sV8lC5/1

Upvotes: 0

Adam
Adam

Reputation: 5233

Why don't you use String.match?

$('#description').keyup(function (e) {

    var descriptionValue = $(this).val().toLowerCase();


    if (descriptionValue.match(new RegExp($('#keyword').val(),'i'))) {
        var value = 'Yes';
        document.getElementById("seocontentshow").style.color = 'limegreen';
    } else {
        var value = 'No';
        document.getElementById("seocontentshow").style.color = 'red';
    }

    checkstatus();
    extrafunctions();

    $('.content-content').text(value);
});

Upvotes: 0

Related Questions