开发者

Javascript can't find strings containing square brackets

I'm using this function that helps my webdevelopers to find a piece of code in a webpage:

function findString (str) {
  var strFound;
  strFound=self.find(str);
  if (strFound && self.getSelection && !self.getSelection().anchorNode ){   
    strFound=self.find(str);
  }
  if (!strFound) {
    strFound=self.find(开发者_开发百科str,1,1)
    while (self.find(str,1,1)) continue
  }
}

The problem is that when I enter the following string for example:

array[1]

It can't find it! Which is strange because I tried this function and It can find any other string. So, how can I use this function to find a string containing square brackets (without using regular expressions if it's possible)?

Thank you,

Regards


I tried you script and there is a bug in it. If you replace the following

if (strFound && self.getSelection && !self.getSelection().anchorNode )  

by the following

if (strFound && self.getSelection && !self.getSelection().anchorNode )  {

it works.

In Firefox and Chrome, strFound is true when the searched string is array[1]. In IE 8, script doesn't work.

EDIT: code I've tested:

$(document).ready(function() {
    findString("array[1]");
});

function findString (str) {
    var strFound;
    strFound=self.find(str);
    if (strFound && self.getSelection && !self.getSelection().anchorNode )  {
        strFound=self.find(str);
    }
    if (!strFound) {
        strFound=self.find(str,1,1)
        while (self.find(str,1,1)) continue
    }
    alert(strFound);
 }


What object function find is called on? Is that a window? Is yes, that what exactly does the function find? According to documentation it is:

The find() method displays a find dialog box when invoked. This allows a user to search for a string in the page from which it was invoked.

Does that mean that HTML tokens are not processed and you're exactly looking for HTML token array[1]? Which browsers is the function supported in?

One way to implement server-side search is to read the whole file (assuming you know the location of the file) and do a simple regular expression using string function search

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜