views:

69

answers:

4

What's the correct syntax to search a .txt file for a keyword in JavaScript?

EDIT: I'm working with a subset of JavaScript called UnityScript in a program called Unity3D. It outputs .exe programs. Here's an example of UnityScript:

import System.IO;

function ReadFile () {
    var sr = new StreamReader(Application.dataPath + "/" + readFilePath);
    var fileContents = sr.ReadToEnd();
    sr.Close();

    var lines = fileContents.Split("~"[0]);
    for (line in lines) {
        Debug.Log (line);
    }
}

I thought that if I could get a function from JavaScript I could import it into my program. I see now that perhaps I was wrong.

Thanks - Elliot Bonneville

+1  A: 

Javascript does not have access to the file system. Not without ActiveX plugins or Flash.

Sounds like you need a desktop application or a powershell script.

Josh Stodola
What about System.IO?
Elliot Bonneville
@Elliot That is *not* Javascript! Java is to Javascript like car is to carpet.
Josh Stodola
Javascript does not have System.IO, javascript is a completely different language to Java. Edit: Oops, Josh, you beat me to it.
Razor Storm
I'm working with a subset of Javascript that has a custom API. I thought if I could get a function for JS it would work in US, the subset. Perhaps I was wrong.
Elliot Bonneville
If it has a custom API, you should be telling *us* how to access the file system.
Josh Stodola
@Josh - It's done.
Elliot Bonneville
+3  A: 

It depends, in modern browsers there are some ways to access locally stored files (meaning on the same machine as the user viewing your webpage). However, if the file is stored on the server side, meaning the machine hosting the website, javascript alone is not enough.

If the file is hosted on the client machine, please take a look here.

However, if the file is hosted on the server machine, you may start an AJAX request to the server, and have the server feed back the text file. (Simply printing the file to STDOUT will send it as a response to the HTTP request).

http://en.wikipedia.org/wiki/Ajax_%28programming%29 http://www.w3schools.com/Ajax/Default.Asp

After you receive the data you can use xmlhttpobject.responsetext.match("keyword") to find whether it exists.

Razor Storm
I'm not actually working in a browser. I'm running an .exe with JS embedded. That being the case, I'm not working with AJAX.
Elliot Bonneville
Oh interesting, can you give a bit more details about how the exe is programmed? It seems like a better idea to use the exe itself to accomplish this task. C/C++/C#/perl/whatever you're using to program the exe is going to be much more fit to do this job than javascript is.
Razor Storm
I'm working with a program called Unity3D actually. It is to my program as Visual is to a C++ app. I'm kinda hacking the system as it's 3D but I'm creating an interface app. Anyways, I ended up working with a subset of Javascript called Unityscript. I thought that if I could get a function from Javascript it would work in US.
Elliot Bonneville
A: 

Hi, Try this:

 function process(url, send, RegExp) { 
                    with(new XMLHttpRequest) { 
                        open((send) ? "POST" : "GET", url , false);
                        setRequestHeader("Content-Type:","text/Plain");
                        send(send);
                            if(readyState == 4)
                                    return RegExp != null ? responseText.match(RegExp) : responseText
                }
        }

    example:

          file.txt : 
           name=frank&id=12&foo=a

           process("file.txt", null, /name=([^&]+).id=(\d+)&foo=([^\n]+)/g)
Jet