Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式和javascript在HTML注释中查找模式_Javascript_Regex - Fatal编程技术网

使用正则表达式和javascript在HTML注释中查找模式

使用正则表达式和javascript在HTML注释中查找模式,javascript,regex,Javascript,Regex,我正在构建一个代码生成器/代码编辑器,我正在尝试做一种服务器端包含但客户端包含的工作。我想使用regex和javascript解析出下面一行中的“file属性”,将代码加载到“included”文件中,然后用它替换整个注释。我不需要帮助,只需加载regEx magic.:) 所以首先找到“文件属性”。 然后用另一个字符串替换整个注释 <!--#include file="footer.html" --> 如果字符串总是这种简单形式,您可以 result = subject.repl

我正在构建一个代码生成器/代码编辑器,我正在尝试做一种服务器端包含但客户端包含的工作。我想使用regex和javascript解析出下面一行中的“file属性”,将代码加载到“included”文件中,然后用它替换整个注释。我不需要帮助,只需加载regEx magic.:)

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

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

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

result = subject.replace(/<!--#include file="([^"]*)"\s*-->/g, "Another string with file name: $1");
result=subject.replace(//g,“另一个文件名为:$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;
}
$pattern='/(.*.*)/s';
$subject='';
if(预匹配($pattern、$subject、$regs)){
$prefix=$regs[1];
$fileName=$regs[2];
$suffix=$regs[3];
//从文件加载数据(自己实现)。
$fileData=loadDataFromFile($fileName)
$MyFinalCompletTestRing=$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) «*»
#(.*)
# 
#选项:点匹配换行符
# 
#匹配下面的正则表达式,并将其匹配捕获到反向引用编号1«(.*.*)»中
#按字面上的«“»匹配字符“”
#匹配单个“空白字符”(空格、制表符和换行符)«\s*»
#在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)«*»
#按字面上的«-->»匹配字符“->”
#匹配任何单个字符«。*»
#在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)«*»

var replace=函数(str,进程){
var regex=//g;
返回str.replace(regex,process);
};
var processFile=函数(注释、文件路径){
返回“文件内容”;
};
var结果=替换(
“一些文本或其他内容”,
进程文件
);

快到了。我试图做的是获取“footer.html”,加载其内容,然后用加载的内容替换注释。有点像服务器端的include,但客户端的include。如果你清楚地问了这个问题……你能更新你的问题(点击“编辑”)并清楚地问你想要答案的问题吗。字符串中是否有多个这样的
include
?用正则表达式解析HTML是一种反模式。可能是的。但这是一种服务器端包含的代码:)并不重要。使用实际的HTML解析器并对注释应用正则表达式更好。HTML代码不在DOM中,而是放在文本区域中。您是否尝试在客户端使用JS进行服务器端包含?听起来它至少会破坏SEO,可能还会破坏其他东西。如果字符串包含的内容超过
,则会失败。另外,这显然不是JavaScript而是PHP。您使用了什么程序来生成正则表达式的解释?@TimPietzcker阅读了以下问题:解析出下面一行中的“文件属性”。至于你的第二句话,你是对的,我确实犯了一个错误。希望有人能找到一个有用的答案,所以我不会删除它。@katspaugh应用程序被调用了,我强烈推荐它用于正则表达式的构建/测试。是的,它会的。最后一个字符串将不包含在该注释之前或之后出现的任何内容。
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
);