2

我正在构建一个代码生成器/代码编辑器,我正在尝试做一种服务器端包含但客户端。我想,使用正则表达式和 javascript,在下面的行中解析出“文件属性”,将代码加载到“包含”文件中,然后用它替换整个注释。我不需要帮助来仅加载正则表达式魔术。:)

所以首先找到“文件属性”。然后用另一个字符串替换整个注释。

<!--#include file="footer.html" -->
4

3 回答 3

4

http://jsfiddle.net/xLW83/

var replace = function (str, process) {
     var regex = /<!--\s*#include\s+file="(.+)".*-->/g;
     return str.replace(regex, process);
};

var processFile = function (comment, filePath) {
    return 'content of the file';
};

var result = replace(
    'some text <!--#include file="footer.html" --> something else',
    processFile
);
于 2012-09-09T14:49:20.480 回答
1

如果字符串总是这种简单的形式,你可以做

result = subject.replace(/<!--#include file="([^"]*)"\s*-->/g, "Another string with file name: $1");
于 2012-09-09T12:58:42.653 回答
0

这是您获取文件名、加载其内容并生成字符串的方式:

$pattern = '/(.*<!--#include\s*file\s*=\s*")(.*?)("\s*-->.*)/s';
$subject = '<!--#include file="footer.html" -->';

if (preg_match($pattern, $subject, $regs)) {
    $prefix = $regs[1];
    $fileName = $regs[2];
    $suffix = $regs[3];

    // Load data from file (implement this by yourself).
    $fileData = loadDataFromFile($fileName)

    $myFinalCompleteString = $prefix . $fileData . $suffix;
}

以下是模式的解释:

# (.*<!--#include\s*file\s*=\s*")(.*?)("\s*-->.*)
# 
# Options: dot matches newline
# 
# Match the regular expression below and capture its match into backreference number 1 «(.*<!--#include\s*file\s*=\s*")»
#    Match any single character «.*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the characters “&lt;!--#include” literally «<!--#include»
#    Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the characters “file” literally «file»
#    Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the character “=” literally «=»
#    Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the character “"” literally «"»
# Match the regular expression below and capture its match into backreference number 2 «(.*?)»
#    Match any single character «.*?»
#       Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
# Match the regular expression below and capture its match into backreference number 3 «("\s*-->.*)»
#    Match the character “"” literally «"»
#    Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the characters “-->” literally «-->»
#    Match any single character «.*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
于 2012-09-09T13:56:24.480 回答