【发布时间】:2012-11-11 01:18:27
【问题描述】:
有人知道javascript中search()函数使用的算法是什么吗?
var myRegExp = /Alex/;
var string1 = "Today John went to the store and talked with Alex.";
var matchPos1 = string1.search(myRegExp);
if(matchPos1 != -1)
document.write("There was a match at position " + matchPos1);
else
document.write("There was no match in the first string");
我需要使用这个函数在文本文档中搜索不同的字符串值。但是我需要记录下这个方法背后的算法是什么,复杂性是什么。否则我必须编写自己的方法来搜索我拥有的文本文件。
【问题讨论】:
-
正则表达式引擎的详细实现...?你为什么要问,你真正想知道什么?
-
String#search的规范可能是最好阅读的内容。 -
我需要使用这个函数在一个文档中搜索不同的字符串。但我需要记录这种方法背后的算法是什么。否则我必须编写自己的方法来搜索我拥有的文本文件。
-
如果不需要正则表达式,使用
String.indexOf(),这是一个线性搜索。
标签: javascript algorithm search