English 中文(简体)
轨道回归指数 而不是计算模式
原标题:Array returning indexOf rather than count of pattern
  • 时间:2011-11-05 14:07:57
  •  标签:
  • javascript

因此,我编篡了我的法典,我 st不 st地试图计算某一具体记录中出现的时间。

如果像<in”这样的东西被搜寻,但现在我得到的却是13,19,-1<>。

我非常赞赏关于如何改进我守则的任何方面。

books = [
    {
    title: "Inheritance: Inheritance Cycle, Book 4",
    author: "Christopher Paolini",
    },
{
    title: "The Sense of an Ending",
    author: "Julian Barnes"},
{
    title: "Snuff Discworld Novel 39",
    author: "Sir Terry Pratchett",
    }
]
search = prompt("Title?");

function count(books, pattern) {

        var num = 0;
        var result = [];
        for (i = 0; i < books.length; i++) {
            var index = books[i].title.toLowerCase().indexOf(pattern.toLowerCase());
            do {
                result[i] = index;
                index = books[i].title.toLowerCase().indexOf(pattern.toLowerCase(), index + 1);
            }
            while (index >= 0)
            num = 0;
        }
        return result;
    }
alert(count(books, search));
最佳回答

你们需要保持这一指数,而不是其结果的指数。 只有当指数大于或等于零时,你才能增加计算。 注I ve还做了一些优化工作,以减少你改变扼杀所需时间。 我也增加了一种使用定期表述的不同版本。

function count(books, pattern) {
    var result = [];
    for (i = 0; i < books.length; i++) {
        var index = -1, // set to -1 so that the first iteration will set it to 0
            title = books[i].title.toLowerCase(),
            realPattern = pattern.toLowerCase();
        result[i] = 0;
        do {
            index = title.indexOf(pattern, index + 1);
            if (index >= 0) {
               result[i] = result[i] + 1;
            }
        }
        while (index >= 0)
    }
    return result;
}

更好的替代选择

RegExp.escape = function(text) {
    return text.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&");
}

function count(books, pattern) {
    var result = [];
    for (i = 0; i < books.length; i++) {
        var regex = new RegExp(RegExp.escape(pattern), gi ),
            matches = books[i].title.match(regex);
        result[i] = matches ? matches.length : 0;
    }
    return result;
}
问题回答

www.un.org/Depts/DGACM/index_spanish.htm 引自:

function count(books, pattern) 
{

var num = 0;
var result = Array();
for (i = 0; i < books.length; i++) 
{
    var times = books[i].title.toLowerCase().split(pattern.toLowerCase());
    if(times.length - 1 >= 0)result[i] = times.length - 1;
}
return result;
}

希望你们能找到这一有益的l!





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.