[英]Get EVERY String between two words
I tried to make a search-bar for my current project. 我尝试为当前项目创建一个搜索栏。
Here is the current HTML: 这是当前的HTML:
<input type="text" id="searchbar"/>
The CSS is for now not important... 目前CSS并不重要...
I made it possible to find files/folders from server: 我可以从服务器查找文件/文件夹:
If you hit enter, JavaScript sends a $.post() 如果您按Enter键,JavaScript将发送$ .post()
Here is my Javascript: 这是我的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: PHP输出:
"folder:****&file:*****&file:*****&"
I read this stackoverflow, to get the foldernames, or file: Get Substring between two characters using javascript 我阅读了此stackoverflow,以获取文件夹名称或文件: 使用javascript获取两个字符之间的子字符串
The Reg-Expression outputs: Reg-Expression输出:
["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 感谢您的帮助:D
The problem you are having is the regex in the split function. 您遇到的问题是split函数中的正则表达式。
Specifically, character classes contain a list of separate characters. 具体来说,字符类包含一个单独字符的列表。
So, [folder:&]
really matches a single character from the set of characters 因此,
[folder:&]
实际上匹配字符集中的单个字符
'f','o','l','d','e','r',':','&'
To split on '&folder:'
or '&'
or ':'
it should be something like this 要在
'&folder:'
或'&'
或':'
上分割,应该是这样的
myfolders = foundedFolders.split(/&?folder:|[&:]+/);
Likewise, for '&file:'
or '&'
or ':'
同样,对于
'&file:'
或'&'
或':'
myfiles = foundedFiles.split(/&?file:|[&:]+/);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.