2

I'm building a code generator/code editor and I'm trying to do a sort of Server Side Include but client side. I want to, using regex and javascript, parse out the "file attribute" in the line below, load the code in the "included" file and then replace the entire comment with that. I don't need help to do the loading only the regEx magic. :)

So first find the "file attribute". Then replace the entire comment with another string.

<!--#include file="footer.html" -->
6
  • Parsing HTML with regex is an anti-pattern. Commented Sep 9, 2012 at 13:07
  • Yes maybe. But this is a Server-Side-Include kind of code :) Commented Sep 9, 2012 at 13:14
  • Doesn't matter. Using an actual HTML parser and applying a regex to the comment is better Commented Sep 9, 2012 at 13:20
  • The HTML code is not in the DOM, its placed in a textarea. Commented Sep 9, 2012 at 13:24
  • Are you trying to do server-side includes using JS on the client side? Sounds like it will break at least SEO, and probably other stuff as well. Commented Sep 9, 2012 at 13:25

3 Answers 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
);
Sign up to request clarification or add additional context in comments.

Comments

1

If the string is always of this simple form, you can do

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

3 Comments

Almost there. What I'm trying to do is get the "footer.html", load its content and then replace the comment with the loaded content. Sort of like server side include but client side.
If only you'd clearly asked that question...could you update your question (click on 'edit') and clearly ask the question to which you want an answer.
Can there be more than one such include in the string?
0

This is how you get your filename, load its content and generate your string:

$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;
}

Here's the explanation of the pattern:

# (.*<!--#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) «*»

6 Comments

This fails if the string contains anything more than <!--#include file="footer.html" -->. Also, this obviously isn't JavaScript but PHP.
What program did you use to generate the explanation of the regex?
@TimPietzcker Read the question: parse out the "file attribute" in the line below. As for your second remark, you're right, I did make a mistake there. Hopefully, someone might find an answer useful, so I will not delete it.
@katspaugh The application is called RegexBuddy and I highly recommend it for regex building/testing.
Yes it will. The final string will not contain anything that occurs before or after that comment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.