Reputation: 22030
I am trying to figure out whether a text box has a given word, regardless of case. For example, how can I determine whether a given text box, #TextBox
, has the word "hello"
in it?
var specialwords = ['hello','Hello','HELLO']; //special words here
$('#TextBox').keydown(function() {
var text = $(this).val();
text = text.split(" ");
var newtext = "";
for(var i = 0; i < text.length; i++){
// code to check words
}
$("#check").html(newtext);
});
Upvotes: 1
Views: 146
Reputation: 9895
You can make a case insensitive regex with all the words like this:
RegExp("\\b(?:" + specialwords.join("|") + ")\\b", "i")
then you can use it against each word. I am not sure what you are doing with the words once you identify them ... I will assume for the purpose of my code snippet that you are ignoring them from the text.
var specialwords = ['hello','world','pizza']; //special words here
var swr = RegExp("\\b(?:" + specialwords.join("|") + ")\\b", "i")
$('#TextBox').keydown(function() {
var text = $(this).val();
text = text.split(" ");
var newtext = [];
for(var i=0; i<text.length; i++){
if (!text.match(swr)) {
newtext.push(text)
}
}
$("#check").html(newtext.join(" "));
});
Using the \b
as a word delimiter in regexp you can also check the whole text without breaking up the words if you want.
var specialwords = ['hello','world','pizza']; //special words here
var swr = RegExp("\\b(?:" + specialwords.join("|") + ")\\b", "i")
$('#TextBox').keydown(function() {
var text = $(this).val();
var newtext = text.replace(swr, "");
$("#check").html(newtext);
});
Upvotes: 1
Reputation: 11171
The easiest way to check whether a text box has a given word, irrespective of case, is to convert the text box to lowercase, then split by spaces and find the indexOf
the word.
var word = "hello".toLowerCase(); // make sure this word is lowercase
$("#TextBox").keydown(function () {
var text = $(this).val().toLowerCase().split(" ");
if (text.indexOf(word) > -1) {
// do something
} else {
// word is not in the text box
}
})
If you want to check for an array of words, specialWords
, wrap the if block in a for loop. This would be O(n²)
complexity, but that should be fine, as long as your input isn't extremely long1.
1we're talking thousands upon thousands of words long for it to matter.
Upvotes: 2
Reputation: 25352
TRY this
$(document).ready(function(){
var specialwords = ['hello','Hello','HELLO'];//special words here
$('#TextBox').keydown(function() {
//alert(this);
var text = $(this).val().trim();
console.log(text);
// text = text.trim().split(" ");
var newtext = "";
var up=0,low=0;
for(var i=0;i<text.length;i++){
if(text[i]==" ")
continue;
else{
if(text[i].trim()==text[i].trim().toLowerCase())
low++;
if(text[i].trim()==text[i].trim().toUpperCase())
up++;
}
}
if(up>0 && low>0)
newtext="mix case";
else if(up>0)
newtext="Upper case";
else if(low>0)
newtext="Lower case"
$("#check").html(newtext);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="TextBox">
<div id="check"></div>
Upvotes: 1
Reputation: 4519
function checkAlphaCase(alpha) {
if (alpha == alpha.toLowerCase()) {
alert('LowerCase');
} else if (alpha == alpha.toUpperCase()) {
alert('UppperCase');
} else {
alert('MixedCase');
}
}
checkAlphaCase('ANIR');
Upvotes: 1