Javascript
Friends with relevant experience know that JScript is limited in efficiency after all, when looking up data in an array, it is slow to perform with conventional algorithms.
For example, in a 500-string data array, we want to find a specified character (key) and return its array subscript, if this algorithm is used:
CODE:
function Usual_search (Data,key)
{
var m=data.length
for (i=0;i<m;i++)
{if (Data[i]==key) return i}
}
Because of the need to do multiple comparisons, the operation will be quite slow.
This topic describes a way to take advantage of the JScript built-in method for finding data in an array, which is much more efficient than the conventional algorithm, thanks to the JScript built-in approach. For the sake of (witty | bluffing), I called "JS oppressive search Method".
This lookup method has a requirement for an array element: The contents of an array element must not contain a half-width comma (,) and one of the tokens we specify (for example, in the following example, we specify that the surrogate symbol is a tab "┢"). Be aware of this requirement when building and maintaining arrays in advance.
JS Brutal Search method is very simple, the principle of only one, is to "take full advantage of the JScript built-in method":
We first make use of the array object's ToString () method to produce a string containing the elements of the array, in which each array element is delimited by a half-width comma (,), so we require that the contents of the array element must not contain a half-width comma.
The string object's replacement () method is then used to replace the key string that we are looking for in the strings with a special symbol (the token) that we specify. Generally choose a character that is not commonly used to act as a surrogate symbol, in the following example I used a tab character (┢), As long as you are able to ensure that the symbols that do not appear in the array elements can act as surrogate symbols.
Next comes our most brutal step, or use the Replace () method to remove all characters except the half-width comma (,) and the ┢. Everything goes clean. This string becomes a series of half-width commas containing a surrogate symbol (this:,,,,,,,,,,,,,,,, ┢,,,,,,,,,).
Finally, the IndexOf () method of the String object returns the position of the surrogate symbol in the string, which is precisely the array subscript in the original array.
JScript Sample Program:
Run Code Box
<script>function Js_cruel_search (data,key)/*js brutality lookup */{re = new RegExp (key,[""]) return (Data.tostring (). Rep Lace (Re, "┢"). Replace (/[^,┢]/g, "")). IndexOf ("┢")}function Show () {P=dataword.value.split (",") key= Keyword.valueresult=js_cruel_search (P,key) if (result>-1) {alert ("" "+key+" is located in the position "+ (result+1) +". }else{alert ("Not Found!") "}}</script>data:<input Name=dataword readonly value=" In, Your, eyes, end, ignorant, the day, make single, Chingru, old, earn, tie, refused, involved, vulgar flow, Honghu, borrowed, Had he, the wing, the bladder, may, who, can help, he, fly? "Size=120><p>key:<input name=keyword readonly value= honghu ><p><button () > Find </button>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]