node.js Javascript检查(txt)文件是否包含字符串/变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17449133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript check if (txt) file contains string/variable
提问by Mdlc
I wondering if it's possible to open a text file with javascript (location something like: http://mysite.com/directory/file.txt) and check if the file contains a given string/variable.
我想知道是否可以使用 javascript 打开文本文件(位置类似于:http: //mysite.com/directory/file.txt)并检查文件是否包含给定的字符串/变量。
In php this can be accomplished very easy with something like:
在 php 中,这可以很容易地完成,例如:
$file = file_get_contents("filename.ext");
if (!strpos($file, "search string")) {
echo "String not found!";
} else {
echo "String found!";
}
Is there a, preferably easy, way to do this? (I'm running the "function" in a .js file on nodejs, appfog, if it might be necessarily).
有没有一种,最好是简单的方法来做到这一点?(我正在 nodejs 上的 .js 文件中运行“函数”,appfog,如果可能的话)。
回答by raam86
You can not open files client side with javascript.
您无法使用 javascript 打开文件客户端。
You can do it with node.js though on the server side.
尽管在服务器端,您可以使用 node.js 来完成。
fs.readFile(FILE_LOCATION, function (err, data) {
if (err) throw err;
if(data.indexOf('search string') >= 0){
console.log(data)
}
});
Newer versions of node.js (>= 6.0.0) have the includesfunction, which searches for a match in a string.
较新版本的 node.js (>= 6.0.0) 具有includes在字符串中搜索匹配项的功能。
fs.readFile(FILE_LOCATION, function (err, data) {
if (err) throw err;
if(data.includes('search string')){
console.log(data)
}
});
回答by mh-cbon
You may also consider to use a stream, because it can handle larger files.
您也可以考虑使用流,因为它可以处理更大的文件。
var fs = require('fs');
var stream = fs.createReadStream(path);
var found = false;
stream.on('data',function(d){
if(!found) found=!!(''+d).match(content)
});
stream.on('error',function(err){
then(err, found);
});
stream.on('close',function(err){
then(err, found);
});
Error or close will occur, it will then close the stream becasue autoClose is true, the default vallue.
将发生错误或关闭,然后将关闭流,因为 autoClose 为 true,默认值。
回答by Abdennour TOUMI
OOP way :
面向对象方式:
var JFile=require('jfile');
var txtFile=new JFile(PATH);
var result=txtFile.grep("word") ;
//txtFile.grep("word",true) -> Add 2nd argument "true" to ge index of lines which contains "word"/
Requirement :
要求 :
npm install jfile
Brief :
简短的 :
((JFile)=>{
var result= new JFile(PATH).grep("word");
})(require('jfile'))
回答by Francois
From the client side you can definitely do this :
从客户端你绝对可以这样做:
var xhttp = new XMLHttpRequest(), searchString = "foobar";
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
console.log(xhttp.responseText.indexOf(searchString) > -1 ? "has string" : "does not have string")
}
};
xhttp.open("GET", "http://somedomain.io/test.txt", true);
xhttp.send();
If you want to do it on the server side with node.js use File System package this way :
如果你想在服务器端用 node.js 做这件事,请使用文件系统包:
var fs = require("fs"), searchString = "somestring";
fs.readFile("somefile.txt", function(err, content) {
if (err) throw err;
console.log(content.indexOf(searchString)>-1 ? "has string" : "does not have string")
});
回答by Bergi
Is there a, preferably easy, way to do this?
有没有一种,最好是简单的方法来做到这一点?
Yes.
是的。
require("fs").readFile("filename.ext", function(err, cont) {
if (err)
throw err;
console.log("String"+(cont.indexOf("search string")>-1 ? " " : " not ")+"found");
});

