jQuery:用字符串搜索文本

时间:2012-09-16 14:20:48

标签: javascript jquery

我试试这个:

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

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

但是有这个错误:

  

TypeError:$(form.comment).search不是函数

我解决了这个问题:

if(form['comment'].value.search('<Вашият отговор тук>') != -1)
    alert(1)
else
    alert(0)

2 个答案:

答案 0 :(得分:3)

你想要的是$(form['comment']).text().search()

所以:

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

答案 1 :(得分:0)

要找出匹配字符串的其他内容,您不需要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.