簡體   English   中英

使用javascript在txt文件中搜索

[英]Search inside txt file using javascript

www.example.com/page_a/上是否可以運行 javascript 代碼來搜索位於www.example.com/page_b/my_file.txt的文件中的文本“hello”?

javascript/jquery/任何其他javascript框架可以做到這一點嗎?

是的,這是可能的,而且使用 jquery 最簡單。

您需要像這樣“獲取”文本文件的內容:

$.get("www.example.com/page_b/my_file.txt", function(contents){
   //contents variable now contains the contents of the textfile as string

   //check if file contains the word Hello
   var hasString = contents.includes("Hello");

   //outputs true if contained, else false
   console.log(hasString);
})

嘗試使用 jquery $.get()

$.get("text file path",function(textString) {
    // handle the returned string ..
},"text/plain")

我正在使用 indexOf() 在使用 JavaScript 的文本文件中搜索字符串。 這是代碼

$.get("www.example.com/page_b/my_file.txt", function(contents){
   var str = "Hello";
   if(contents.indexOf(str) != -1){
       console.log(str + " found");
   }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM