在Java脚本中从文本文件中搜索字符串 [英] Search for a string from a text file in java script

查看:142
本文介绍了在Java脚本中从文本文件中搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java Script创建用于移动设备的Dictionary应用程序.我已将Dictionary源存储在一个单独的文件中.我的疑问是如何使用Java Script访问该文件并搜索特定的字符串.

I am creating a Dictionary app for mobile using Java Script. I have stored Dictionary source in a separate file. My doubt is how to access that file and search for particular string using Java Script.

function dic() {
    var word = document.getElementById("wo").value;
    var dictionary = new Array("Java","Python","Swift","HTML","PHP");
    var flag = 0;
    for(var i = 0; i < dictionary.length; i++) {
        if(dictionary[i] == word) {
            document.getElementById("result").innerHTML = "Element Found";
            flag = 1;
            break;
        }
    }
    if(flag == 0)
        document.getElementById("result").innerHTML = "Element Not Found";  

}

此脚本检查数组中的单词.我想将数组单词替换为文本文件.预先感谢

This script checks for the word in array. I want to replace the array words to a text file. Thanks in advance

推荐答案

要访问文件,请使用jQuery的.get()函数.这也可以在纯JS中使用XMLHttpRequests完成,但是jQuery将为您处理跨浏览器的兼容性.

To access the file, use jQuery's .get() function. This can also be done with XMLHttpRequests in pure JS but jQuery will handle cross-browser compatibilty for you.

要获取文件:

$.get("http://www.example.com/path/to/file.txt",function(returnedData) {
    $("#element").text(returnedData);
},"text/plain");

这将加载 http://www.example.com/path/的内容到/file.txt 到ID为elementdiv中.假设每个词典单词都在换行符上,则可以使用以下命令将它们分成一个数组:

This will load the contents of http://www.example.com/path/to/file.txt into the div with ID element. Assuming each dictionary word is on a new line, you can then separate them into an array with the following:

var text = $("#element").text();
var words = text.split("\n");
var dictionary = new Array();
for(var i=0; i < words.length; i++) {
    dictionary[i] = words[i];
}

现在,您的数组中有字典单词,可以使用现有代码(经过一些更正)搜索它们:

Now you have the dictionary words in your array and can search for them with your existing code (with a couple of corrections):

function dic() {
    var word = document.getElementById("wo").value;
    // var dictionary = new Array("Java","Python","Swift","HTML","PHP");
    // This line is unnecessary; use the dictionary var already created.
    var flag = 0;
    for(var i = 0; i < dictionary.length; i++) {
        if(dictionary[i] == word) {
            document.getElementById("result").innerHTML = "Element Found";
            flag = 1;
            break;
        }
        if(i == dictionary.length - 1) {
            document.getElementById("result").innerHTML = "Element Not Found"; 
        }
    }
}

那应该给你正确的答案.

That should leave you with the right answer.

希望这会有所帮助.


我的第二个代码示例中的部分代码是不必要的,只是为了清楚起见;您实际上可以只声明var dictionary = text.split("\n");,它会产生相同的结果.


Part of the code in my second code sample is unnecessary and only there for clarity; you can in fact just state that var dictionary = text.split("\n");, which will yield the same result.

这篇关于在Java脚本中从文本文件中搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆