JavaScript 无法找到包含方括号的字符串
我正在使用这个函数来帮助我的网络开发人员在网页中查找一段代码:
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]
它找不到它!这很奇怪,因为我尝试了这个函数,它可以找到任何其他字符串。 那么,如何使用这个函数来查找包含方括号的字符串(如果可能的话,不使用正则表达式)?
谢谢,
问候
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我试过你的脚本,里面有一个错误。如果将以下内容替换
为以下内容,
则可以正常工作。
在 Firefox 和 Chrome 中,当搜索的字符串为 array[1] 时,strFound 为 true。
在 IE 8 中,脚本不起作用。
编辑:我测试过的代码:
I tried you script and there is a bug in it. If you replace the following
by the following
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:
find 调用了什么对象函数?那是一扇窗户吗?是的,函数
到底找到了什么
?根据文档,它是:这是否意味着 HTML 令牌未被处理,并且您正在寻找 HTML 令牌数组 [1]?哪些浏览器支持该功能?
实现服务器端搜索的一种方法是读取整个文件(假设您知道文件的位置)并使用字符串函数
search
执行简单的正则表达式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: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