Instead, you can use .indexOf() to perform a search.
If the string is not found, it returns -1 . If it is found, it returns the first null index where it was located.
var text = this.text; var term = "SEARCHTERM"; if( text.indexOf( term ) != -1 ) alert(term);
If you were hoping for an exact match, which apparently implies using ^ and $ , you can simply compare === .
var text = this.text; var term = "SEARCHTERM"; if( text === term ) alert(term);
EDIT:. Based on your comment, you want to get an exact match, but === doesn't work, and indexOf() -. This sometimes happens if there are some spaces that need to be trimmed.
Try trimming spaces with the jQuery jQuery.trim() method.
var text = $.trim( this.text ); var term = "SEARCHTERM"; if( text === term ) alert(term);
If this does not work, I recommend logging this.text in the console to find out if this is the expected value.
user113716
source share