Yannick
Yannick

Reputation: 19

Call certain words in a string with and without capitals

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

Answers (3)

LellisMoon
LellisMoon

Reputation: 5020

var text="Cheese cheese CHEESE",
regex= new RegExp('cheese', 'gi');
console.log(text.match(regex).length);

Upvotes: 0

Stefano Zanini
Stefano Zanini

Reputation: 5916

You can apply .toLowerCase() to both textFocus and the keyword.

Upvotes: 0

Alex
Alex

Reputation: 23300

if you use /ig instad of /g in your .match(), it becomes case-insensitive

Upvotes: 1

Related Questions