Search code examples
unity-game-engineunityscript

How can I search a .txt file for a keyword in Unityscript?


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


Solution

  • 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
    

    Call the function like

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