【问题标题】:Get EVERY String between two words获取两个单词之间的每个字符串
【发布时间】:2014-11-15 15:48:12
【问题描述】:

我试图为我当前的项目制作一个搜索栏。

这是当前的 HTML:

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

CSS 暂时不重要...

我可以从服务器上找到文件/文件夹:

如果你按下回车,JavaScript 会发送一个 $.post()

这是我的 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.");
});

我的问题在这里:

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

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

PHP-输出:

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

我阅读了这个 stackoverflow,以获取文件夹名称或文件:Get Substring between two characters using javascript

正则表达式输出:

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

但通常应该是:

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

需要这些“文件名”来显示它们..

感谢您的帮助:D

【问题讨论】:

    标签: javascript php regex string substring


    【解决方案1】:

    您遇到的问题是 split 函数中的正则表达式。
    具体来说,字符类包含一个单独的字符列表。

    所以,[folder:&amp;] 真正匹配 集合 字符中的单个字符
    'f','o','l','d','e','r',':','&amp;'

    要在'&amp;folder:''&amp;'':' 上拆分,应该是这样的

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

    同样,对于'&amp;file:''&amp;'':'

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

    【讨论】:

    • 谢谢它现在可以工作了:D 我以为这些是字符,但我不明白这怎么可能实现......谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 2012-04-13
    • 2021-10-14
    • 1970-01-01
    • 2013-12-12
    • 2015-04-08
    • 2012-12-28
    相关资源
    最近更新 更多