9

I have a variable that contains some text, some html, basically can be a string. I need to search the variable for a specific string to process that variable differently if it is contained. Here is a snippet of what I am trying to do, does not work obviously :)

$.each(data.results,
   function(i, results) {
   var text = this.text                   
   var pattern = new RegExp("^[SEARCHTERM]$");
    if(pattern.test( text ) )
    alert(text);  //was hoping this would alert the SEARCHTERM if found...
1
  • what it say when you do a alert(this.text)? Commented Jan 3, 2011 at 2:09

3 Answers 3

17

You could use .indexOf() instead to perform the search.

If the string is not found, it returns -1. If it is found, it returns the first zero-based 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 your use of ^ and $ seems to imply, you could just do an === comparison.

var text = this.text;
var term = "SEARCHTERM";

if( text === term )
    alert(term);

EDIT: Based on your comment, you want an exact match, but === isn't working, while indexOf() is. This is sometimes the case if there's some whitespace that needs to be trimmed.

Try trimming the whitespace using jQuery's jQuery.trim() method.

var text = $.trim( this.text );
var term = "SEARCHTERM";

if( text === term )
    alert(term);

If this doesn't work, I'd recommend logging this.text to the console to see if it is the value you expect.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, so I am looking for an exact match to the search term, but the === comparison did not seem to work. Using indexOf, however, does. Why would that be?
@thatryan: It would depend on the value of this.text. Without knowing what data.results holds, I can't say for sure, but I'd guess that perhaps there's some whitespace that needs to be trimmed. The indexOf() will locate any partial match in the string, so it may give false positives at times, while === will fail if there's any difference, including whitespace. I updated my answer.
I did Patrick, thanks. Sorry I thought I had checked it as answered! Thank you for all your help!
2

If you are using a regular expression to search for a substring, you can find the match like this:

var match = text.match(pattern);
if(match) {
    alert(match[0]);
}

See the documentation for .match() for more information.

Note that if you are trying to search for the string "[SEARCHTERM]" (a search term containing brackets), your search method will not work because the brackets have a special meaning within regular expressions. Instead, use .indexOf():

var index = text.indexOf("[SEARCHTERM]");
if(index != -1) {
    alert("Search term found");
}

However, if you are just trying to find an exact match (nothing else but the search term is in the string you are checking), you should just compare the strings:

if(text == "[SEARCHTERM]") {
    alert("Search term found");
}

Comments

0

What does "does not work" mean here?

Be aware that you should use braces ({}) with if statements. Some browsers will allow you to miss them out but you should include them to be correct and to support everything.

3 Comments

Oh, and you're missing a semi-colon on the line in which you initialise the variable test.
"Some browsers will allow you..." Excluding them is actually allowed by the language specification, but certainly good practice to include them.
@patrick dw: Ah, oops! Thanks for that. I admit I'd always just assumed; still, widespread browsers like IE6 will not be happy with it IME, so best to keep them in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.