3

I try this:

form['comment'] = '<blockquote>test</blockquote><a href="#">test</a> <Your reply here>';

if($(form['comment']).search('<Your reply here>'))
    alert(1)
else
    alert(0)

But have this error:

TypeError: $(form.comment).search is not a function

I fix this problem that:

if(form['comment'].value.search('<Вашият отговор тук>') != -1)
    alert(1)
else
    alert(0)
5
  • Did you include the jquery js file? Commented Sep 16, 2012 at 14:22
  • Yes, I include jquery.js file Commented Sep 16, 2012 at 14:24
  • 1
    search is the method of String, not jQuery object. Commented Sep 16, 2012 at 14:24
  • …and it does search for regular expressions, not for substrings Commented Sep 16, 2012 at 16:48
  • Rather than edit the title with [RESOLVED] or similar, mark the correct answer as accepted by clicking the checkmark beside it. Commented Sep 24, 2012 at 13:10

2 Answers 2

3

What you want is $(form['comment']).text().search()

So:

form = [];
form['comment'] = '<blockquote>test</blockquote><a href="#">test</a> <Your reply here>';

if(form['comment'].search('<Your reply here>') != -1)
    alert(1)
else
    alert(0)​

Here's a working example.

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

3 Comments

It will always alert 1 because in that example, there is always "<Your reply here>" in the text.
But when I remove <Your reply here> form text again alert 1 :)
How to get start and end index of search results?
0

To find out that is matching string to other you don't need jQuery!.

var comment = "<blockquote>test</blockquote><a href="#">test</a> <Your reply here>",
    search = "<Your reply here>";


comment.indexOf( search ) != -1;  //  indexOf returns -1 when search does not match
/<Your reply here>/.test( comment ); // returns true when search is in comment. 

Comments

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.