Javascript找不到包含方括号的字符串
我使用此函数帮助我的Web开发人员在网页中查找一段代码:Javascript找不到包含方括号的字符串,javascript,string,square-bracket,Javascript,String,Square Bracket,我使用此函数帮助我的Web开发人员在网页中查找一段代码: function findString (str) { var strFound; strFound=self.find(str); if (strFound && self.getSelection && !self.getSelection().anchorNode ){ strFound=self.find(str); } if (!strFound) { s
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
}
}
问题是,例如,当我输入以下字符串时:
array[1]
它找不到它!这很奇怪,因为我尝试了这个函数,它可以找到任何其他字符串。
那么,如果可能的话,我如何使用这个函数在不使用正则表达式的情况下查找包含方括号的字符串呢
谢谢,
关于调用了什么对象函数find?那是窗户吗?是的,那函数到底找到了什么?根据文件,它是: find方法在调用时显示一个find对话框。 这允许用户在调用字符串的页面中搜索字符串。 这是否意味着HTML标记未被处理,而您正在查找HTML标记数组[1]?中支持哪些浏览器的功能
实现服务器端搜索的一种方法是,假设您知道文件的位置,读取整个文件,并使用字符串函数搜索执行简单的正则表达式。我尝试了您编写的脚本,但其中存在错误。如果更换以下部件:
if (strFound && self.getSelection && !self.getSelection().anchorNode )
通过以下方式
if (strFound && self.getSelection && !self.getSelection().anchorNode ) {
它起作用了
在Firefox和Chrome中,当搜索字符串为数组[1]时,strFound为true。
在IE8中,脚本不起作用
编辑:我测试过的代码:
$(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);
}
我编辑了这篇文章。我忘记了开头的支架,但即使这样,它也不起作用。你能粘贴你的代码吗?谢谢。函数findString str{var strFound;strFound=self.findstr;if strFound&&self.getSelection&!self.getSelection.anchorNode{strFound=self.findstr;}if!strFound{strFound=self.findstr,1,1而self.findstr,1,1 continue}alertstrFound;}$document.readyfunction{findStringarray[1];};我正在使用Firefox 3.6.8。您是对的,搜索功能很有用,但是查找功能不仅可以查找字符串,还可以滚动到其中并高亮显示。这就是为什么我要让它与find函数一起工作。