簡體   English   中英

使用正則表達式和JavaScript在HTML注釋中查找模式

[英]Find a pattern in an HTML comment using regex and javascript

我正在構建代碼生成器/代碼編輯器,並且嘗試執行某種服務器端包含但客戶端的操作。 我想使用正則表達式和javascript解析出以下行中的“文件屬性”,將代碼加載到“包含”文件中,然后將整個注釋替換為該文件。 我不需要幫助來僅加載regEx魔術。 :)

因此,首先找到“文件屬性”。 然后,用另一個字符串替換整個注釋。

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

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
);

如果字符串始終是這種簡單形式,則可以

result = subject.replace(/<!--#include file="([^"]*)"\s*-->/g, "Another string with file name: $1");

這是獲取文件名,加載文件內容並生成字符串的方式:

$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 “<!--#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) «*»

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM