0

I tried to make a search-bar for my current project.

Here is the current HTML:

<input type="text" id="searchbar"/>

The CSS is for now not important...

I made it possible to find files/folders from server:

If you hit enter, JavaScript sends a $.post()

Here is my Javascript:

$.post( "files/openFolder.php",{folder : $searchtext}, function() {
})
.success(function(data) {
    if(data != "") {
        if(data.indexOf("folder:") > -1){ //contains folders..!
            foundedFolders = data;
            myfolders = foundedFolders.split(/[folder:&]/);
            myfolders = cleanArray(myfolders);
            generateFolderBoxes(myfolders);
            folderBoxSettings();
        }else if(data.indexOf("file:") > -1){ // contains files..!
            foundedFiles = data;
            myfiles = foundedFiles.split(/[file:&]/);
            myfiles = cleanArray(myfiles); //cleans the Array from empty Strings.
            generateFileBoxes(myfiles);
            fileBoxSettings();  
        }

    }
})
.fail(function() {
    alert("Sorry doesn't worked, try later again.");
});

My problem is here:

myfolders = foundedFolders.split(/[folder:&]/);

and

myfiles = foundedFiles.split(/[file:&]/);

PHP-Output:

"folder:****&file:*****&file:*****&"

I read this stackoverflow, to get the foldernames, or file: Get Substring between two characters using javascript

The Reg-Expression outputs:

["such", " - Kop", ".txt", "such", ".txt", "t", "st - Kop", ".txt", "t", "st.txt"] 

But normaly it should be:

["suche - Kopie.txt","suche.txt","test - Kopie.txt","test.txt"]

These "filenames" are needed to display them..

Thanks for helping :D

Community
  • 1
  • 1

1 Answers1

0

The problem you are having is the regex in the split function.
Specifically, character classes contain a list of separate characters.

So, [folder:&] really matches a single character from the set of characters
'f','o','l','d','e','r',':','&'

To split on '&folder:' or '&' or ':' it should be something like this

myfolders = foundedFolders.split(/&?folder:|[&:]+/);  

Likewise, for '&file:' or '&' or ':'

myfiles = foundedFiles.split(/&?file:|[&:]+/);  
  • Thanks it workes now :D I thought that these are characters, but I didn't understood how this could be realizable... THANKS!! – OetziOfficial Nov 15 '14 at 22:02