最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery: Search text in string - Stack Overflow

programmeradmin1浏览0评论

I try this:

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

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

But have this error:

TypeError: $(formment).search is not a function

I fix this problem that:

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

I try this:

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

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

But have this error:

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

I fix this problem that:

if(form['ment'].value.search('<Вашият отговор тук>') != -1)
    alert(1)
else
    alert(0)
Share Improve this question edited Sep 24, 2012 at 13:10 Michael Berkowski 271k47 gold badges450 silver badges394 bronze badges asked Sep 16, 2012 at 14:20 DefenseDefense 2594 silver badges21 bronze badges 5
  • Did you include the jquery js file? – rekire Commented Sep 16, 2012 at 14:22
  • Yes, I include jquery.js file – Defense Commented Sep 16, 2012 at 14:24
  • 1 search is the method of String, not jQuery object. – xdazz Commented Sep 16, 2012 at 14:24
  • …and it does search for regular expressions, not for substrings – Bergi 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. – Michael Berkowski Commented Sep 24, 2012 at 13:10
Add a ment  | 

2 Answers 2

Reset to default 3

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

So:

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

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

Here's a working example.

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

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


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

评论列表(0)

  1. 暂无评论