Reputation: 19
I'm working on a keyword search thing in javascript. So javascript counts for example the word "cheese" but only without the capitals. So if for example the string is "Cheese cheese CHEESE" it only counts 1. The keyword is achieved from a $_POST['keyword']; with php
how can i do this with javascript...
var textFocus = $('#content').text();
var count = (textFocus.match(/<?php echo $_POST['keyword']; ?>/g) || []).length;
many thanks in advance..
Upvotes: 1
Views: 31
Reputation: 5020
var text="Cheese cheese CHEESE",
regex= new RegExp('cheese', 'gi');
console.log(text.match(regex).length);
Upvotes: 0
Reputation: 5916
You can apply .toLowerCase()
to both textFocus
and the keyword.
Upvotes: 0
Reputation: 23300
if you use /ig
instad of /g
in your .match()
, it becomes case-insensitive
Upvotes: 1