Forum Moderators: open
i.e.
var all_files = new Array();
var all_files_values = new Aray();
all_files = form.hidden_dev_files.value; (grabbed from Perl code)
for (var i=0;i<all_files.lenght;i++){
var pattern = /form.SEARCH.value/g; // can't get value
if(pattern.test(all_files[i])) {
body = body + '<OPTION ';
body = body + 'value=\"' + all_files[i] + '\">' + all_files[i] + '</OPTIONS>\";
}
...
....
I cannot get the var pattern form.SEARCH.value into the /RegExp/
I have tried
srch = form.SEARCH.value and then using
var pattern = /srch/g;
also
var pattern = /eval(form.SEARCH.value)/g;
I cannot get the form value inside the regexp .
I know it is set, because I fall into this portion if form.SEARCH.value!= ''
Thank you for your help.
/blah/ slashes in a RegExp literal will be taken literally. RegExp constructor: for:
/expression/mods do:
new RegExp('expression','mods') Note that you need to escape any backslashes in the expression.
If we're testing for the presence of our variable as a single word (ignoring case)..
var value = "hello"
var testString = "Does this contain hello or not?"
[color=green]// we want:[/color][color=brown] /\bhello\b/i[/color][color=green]*/[/color]
alert( (new RegExp('\\b'+value+'\\b','i')).test(testString) )
but how do I get the form value entered into the
RegExp?
I think more in Perl, so if I was to do this in Perl
it would be:
chop($resp = <STDIN>);
foreach $line (@all_file) {
if ($line =~ /$resp/) {
print "$line";
}
}
so, in this example, the $resp would be the form.SEARCH.value
but in Perl you can place a variable inside the //
for pattern search of that variable.
in javascript:
How do I place the form.SEARCH.value inside the //
to use it as a Pattern match to each all_files?
Thank you.
[color=brown]// create empty array[/color]
var all_files = new Array();
[color=brown]// and another one (typo corrected)[/color]
var all_files_values = new Array();
[color=brown]// all_files variable assigned to string value from form control[/color]
all_files = form.hidden_dev_files.value; The array initially created has been lost,
..and you can't loop a string.
What format does
[color=brown]form.hidden_dev_files.value[/color] have?
Look at this:
form.hidden_dev_files.value
What is "form," and how does it get to your function?
<input type="text" name="SEARCH">
<input type="button" onClick="mySearch(this.form)" value="click">
Now in your function,
function mySearch(form) {
....
}
Note that the form object (this.form) is passed to the function and a reference to it is stored in the variable "form." Now form has a meaning.
Another simple approach,
<form name="searchform">
<input type="text" name="SEARCH">
<input type="button" onClick="mySearch()" value="click">
...
function mySearch() {
var pattern = document.searchform.SEARCH.value;
}
This will also get the value of your field into the function. Note that no value is required to pass to the function. This is simpler to code but pretty much locks this function to this form, whereas the first allows you to re-use the function from many different forms or pages.
Sorry if I've totally misinterpreted the problem. :-)
var all_files = new Array(); //clears the array
var all_files_values = new Array(); //clears
all_files = form.hidden_dev_files.value; //sets array to all files found inside form.hidden_dev_files.value
gathered outside function using Perl.
>The array initially created has been lost,
>..and you can't loop a string.
I disagree, the array is populated with form.hidden_dev_files.value
>What format does form.hidden_dev_files.value have?
This is simply files names from a directory list that could have alpha or numeric characters.
what I am basically doing is grabbing a directories file names and passing them to form.hidden_dev_files.value. The form has a text entry field to where user can enter partial file name and I want to display each of the form.hidden_dev_files.value that matches what was entered into the form text field named 'SEARCH'.
I know form.SEARCH.value is set because , upon SUBMIT, if form.SEARCH.value is '' , it processes the form normally using all other fields. else it falls into this code to display the directory file names in
form.hidden_dev_files.value that is now equal to all_files.
The code works, and will display all_files in a popup, but I cannot get the form.SEARCH.value inside the RegExp var patern to match each all_files inside the 'for loop'.
ex. all_files has filename AEX1450 AER1450 ARX1900 ..
the user places 1900 in form.SEARCH field.
How do I get the var 1900 inside the RegExp //?
If I hardcode 1900 inside the RegExp the display will only list ARX1900 as a choice.
var pattern = /1900/g;
If I place the form.SEARCH it does not work as above. It displays all file names in all_files because there was no match. as you mentioned the RegExp takes it literal.
var pattern = /form.SEARCH.value/g;
RocknBil, thanks for your post.
I am not sure if that will work for what I need.