Does any body know what the algorithm used for the search() function in javascript is?
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");
I need to use this function to search a text document for different string values. But I need to document what the algorithm behind this method is, and what the complexity is. Otherwise I have to write my own method that searches the text file that I have.
The specification says it’s implemented as a regular expression match:
(Section 15.5.4.12 String.prototype.search (regexp)).
This means your question boils down to the regex matching algorithm. But that is not in the specification either, it depends on the implementation:
(Section 15.10.7 Properties of RegExp Instances).
So, if documenting the complexity of that algorithm is really a requirement, I guess you’ll have to write your own method. But keep in mind that, by doing that, you’ll probably come up with something less efficient, and probably dependent on other built-in methods whose complexity is unknown (maybe even
RegExpitself). So, can’t you convince the powers that be that documenting the complexity of a built-in, implementation-dependent js method is not your job?