LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Javascript string search query (https://www.linuxquestions.org/questions/programming-9/javascript-string-search-query-758638/)

Completely Clueless 09-30-2009 05:16 AM

Javascript string search query
 
Hi all,

I've been tring to get this Javascript function:

str.indexOf()

To report the first instance of a predetermined string in a text file, but am having no luck. I know this command is good for literal strings in isolation, but is there no way it can be made to scan an actual file? If not, what command should I be using?

Thanks.

graemef 09-30-2009 08:43 AM

how are you getting the file details into your string?

Completely Clueless 09-30-2009 11:20 AM

Quote:

Originally Posted by graemef (Post 3701811)
how are you getting the file details into your string?

Here's the relevant code snippet:
Code:

<script type="text/javascript">
var str="/home/clueless/documents/textfile.txt";
document.write(str.indexOf("searchterm") + "<br />");
</script>

I've also tried escaping the forward slashes with an extra slash and with and without the quotes, but no joy. It just always seems to interpret

/home/clueless/documents/textfile.txt as a literal string rather than a path to the named file.

smeezekitty 09-30-2009 12:20 PM

Code:

function findocur(var what, var string){
var base = 0;
var tick = 0;
var flag = 0;
for(base = 0;base < string.length;base++){
flag = 1;
for(tick = 0;tick < what.length;tick++){
if(string[base + tick] != what[tick]){flag = 0; break;}
}
if(flag){return base;}
}
return -1;
}

that should do it but my javascript skills are rusty

ntubski 09-30-2009 02:59 PM

Standard Javascript doesn't support file IO, since it would let web sites do nasty things on your computer.

smeezekitty 09-30-2009 05:21 PM

the original poster said nothing about file io

ntubski 09-30-2009 06:24 PM

Quote:

Originally Posted by smeezekitty (Post 3702425)
the original poster said nothing about file io

The original poster asked

Quote:

Originally Posted by Completely Clueless (emphasis added)
but is there no way it can be made to scan an actual file?


smeezekitty 09-30-2009 06:39 PM

true
javascript cannot do server or client file io
if you need it move to PHP

Completely Clueless 10-02-2009 01:00 PM

Thanks, guys. I thought I was going nuts there for a minute!


All times are GMT -5. The time now is 02:56 AM.