Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/labs/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,12 +781,15 @@ function setupInfo() {
};
};
if (info.correct != null) {
if (correct.length > 0) {
if (correctRe.length > 0) {
alert("Error: Info defines correct value but it's overridden.");
} else if (!(info.correct instanceof Array)) {
alert('Error: Info correct hints must be array.');
} else {
correct = info.correct.map((s) => trimNewlines(s));
let correct = info.correct.map((s) => trimNewlines(s));
// Set global variable with compiled correct answer
correctRe = correct.map((s) =>
processRegex(s, `correct answer ${s}`, true));
};
};
}
Expand Down
11 changes: 0 additions & 11 deletions docs/labs/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">

<!-- See create_labs.md for how to create your own lab! -->

<!-- Sample expected answer -->
<script id="expected0" type="plain/text">
console.log("Hello, world!");
</script>

<!-- Full pattern of correct answer -->
<script id="correct0" type="plain/text">
\s* console \. log \( (["'`])Hello,\x20world!\1 \) ; \s*
</script>

</head>
<body>
<!-- For GitHub Pages formatting: -->
Expand Down
4 changes: 4 additions & 0 deletions docs/labs/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ info =
examples: [ [ " console.log(\"Hello, world!\") " ] ]
}
],
expected: ['console.log("Hello, world!");'],
correct:
[String.raw`\s* console \. log \(
(["'${BACKQUOTE}])Hello,\x20world!\1 \) ; \s*`],
successes: [
[ " console . log( \"Hello, world!\" ) ; " ],
[ " console . log( 'Hello, world!' ) ; " ],
Expand Down
163 changes: 1 addition & 162 deletions docs/labs/regex1.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="checker.css">
<script src="js-yaml.min.js"></script>
<script src="checker.js"></script>
<script src="regex1.js"></script>
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">

<!-- See create_labs.md for how to create your own lab! -->
Expand Down Expand Up @@ -85,168 +86,6 @@
\\z
</script>

<script id="info" type="application/yaml">
---
hints:
- present: |-
/
text: In JavaScript a constant regular expression is surrounded by
forward slashes like /PATTERN/. However, for this exercise we only
want the text inside the slashes (the pattern itself).
examples:
- - "/"
- present: |-
["'`]
text: In this exercise we only want the regular expression pattern itself.
There is no need to use any kind of quote mark.
examples:
- - "'"
- absent: |-
^\^
text: For input validation, start with '^' to indicate a full match.
examples:
- - "(Y|N)"
- present: |-
\\[Zz]
text: The ECMAScript (JavaScript) specification does not support \Z or \z.
examples:
- - "^Y|N\\z"
- absent: |-
\$$
text: For input validation, end with '$' to indicate a full match.
examples:
- - "^(Y|N)"
- absent: |-
[\|\[]
text: Consider using [YN], to match either a Y or an N.
examples:
- - "^$"
- present: |-
\|
absent: |-
\(
text: If you use "|" you must parentheses or the precedence will be wrong.
For example, "^A|B$" accepts anything beginning with A, and it also
accepts anything ending with B. That is not what you want.
examples:
- - "^Y|N$"
- present: " "
text: Spaces normally match spaces in a regex. Do not use them in this case,
because a space is not one of the allowed characters.
examples:
- - "^[YN] $"
- absent: |-
^\^
index: 1
text: For input validation, start with '^' to indicate a full match.
examples:
-
- "^[YN]$"
- ""
- absent: |-
\$$
index: 1
text: For input validation, end with '$' to indicate a full match.
examples:
-
- "^[YN]$"
- "^"
- absent: |-
\[A-Z\]
index: 1
text: You can use [A-Z] to match one uppercase Latin letter (A through Z).
examples:
-
- "^[YN]$"
- "^$"
- present: |-
\^\[A-Z\]\*
index: 1
text: A "*" matches one or more, not one or more.
- present: |-
\(
index: 1
text: You do not need parentheses to solve this problem.
- absent: |-
(\[A-Z\]\+|
\[A-Z\]\[A-Z\]\*)
index: 1
text: You can use [A-Z]+ to match one or more uppercase Latin letters.
examples:
-
- "^[YN]$"
- "^[A-Z]$"
- present: "True"
index: 2
text: Regular expressions are case-sensitive by default; use "true".
- present: "False"
index: 2
text: Regular expressions are case-sensitive by default; use "false".
- absent: |-
\|
index: 2
text: Use "|" to express alternatives.
- present: |-
^\^true\|false\$$
index: 2
text: No. This would match anything beginning with the term true,
or anything ending with the term false. Use parenthesis.
- present: |-
^\^false\|true\$$
index: 2
text: No. This would match anything beginning with the term false,
or anything ending with the term true. Use parenthesis.
- absent: |-
\(
index: 2
text: Use parentheses.
- present: |-
\$
index: 3
text: This is Python, not ECMAScript (JavaScript). Use \Z at the end, not $.
- present: |-
\\z
index: 3
text: This is Python. Use \Z at the end, not \z.
- absent: |-
^\\A
index: 4
text: This is Ruby. Use \A at the beginning.
- absent: |-
\\z$
index: 4
text: This is Ruby. Use \z at the beginning.
- absent: |-
\[A-Z\]
index: 4
text: Use [A-Z] to match one uppercase Latin letter.
- present: |-
\[A-Z\](\*|\+)
index: 4
text: In this case we are only matching one letter, not many of them.
Do not use "*" or "+" after [A-Z].
# successes:
# - - " query ( 'id' ) . isInt ( {min: 1 , max: 9999 } ) ,"
# - - " query ( `id` ) . isInt ( {min: 1 , max: 9_999 } ) , "
# - - 'query ( "id" ) . isInt ( {min: 1 , max: 9_999 } ) ,'
# failures:
# - - " query,"
# - - 'query(''id'').isint({min: 1, max: 9999})'
# - - 'query(''id'').isInt({min: 1, max: 9999})'
#
# Remove all whitespace, so we can make our patterns easier to read
#
preprocessing:
-
- |-
\s*
- ""
# -
# - |-
# (\\s\*)?\s+(\\s\*)?
# - "\\s*"
# debug: true
</script>
<!--

-->
Expand Down
171 changes: 171 additions & 0 deletions docs/labs/regex1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
info =
{
hints: [
{
present: "/",
text: "In JavaScript a constant regular expression is surrounded by forward slashes like /PATTERN/. However, for this exercise we only want the text inside the slashes (the pattern itself).",
"examples": [
[ "/" ]
]
},
{
present: "[\"'`]",
text: "In this exercise we only want the regular expression pattern itself. There is no need to use any kind of quote mark.",
"examples": [
[ "'" ]
]
},
{
absent: String.raw`^\^`,
text: "For input validation, start with '^' to indicate a full match.",
"examples": [
[ "(Y|N)" ]
]
},
{
present: String.raw`\\[Zz]`,
text: "The ECMAScript (JavaScript) specification does not support \\Z or \\z.",
"examples": [
[ "^Y|N\\z" ]
]
},
{
absent: String.raw`\$$`,
text: "For input validation, end with '$' to indicate a full match.",
"examples": [
[ "^(Y|N)" ]
]
},
{
absent: String.raw`[\|\[]`,
text: "Consider using [YN], to match either a Y or an N.",
"examples": [
[ "^$" ]
]
},
{
present: String.raw`\|`,
absent: String.raw`\(`,
text: "If you use \"|\" you must parentheses or the precedence will be wrong. For example, \"^A|B$\" accepts anything beginning with A, and it also accepts anything ending with B. That is not what you want.",
"examples": [
[ "^Y|N$" ]
]
},
{
present: " ",
text: "Spaces normally match spaces in a regex. Do not use them in this case, because a space is not one of the allowed characters.",
"examples": [
[ "^[YN] $" ]
]
},
{
absent: String.raw`^\^`,
"index": 1,
text: "For input validation, start with '^' to indicate a full match.",
"examples": [
[ "^[YN]$", "" ]
]
},
{
absent: String.raw`\$$`,
"index": 1,
text: "For input validation, end with '$' to indicate a full match.",
"examples": [
[ "^[YN]$", "^" ]
]
},
{
absent: String.raw`\[A-Z\]`,
"index": 1,
text: "You can use [A-Z] to match one uppercase Latin letter (A through Z).",
"examples": [
[ "^[YN]$", "^$" ]
]
},
{
present: String.raw`\^\[A-Z\]\*`,
"index": 1,
text: "A \"*\" matches one or more, not one or more."
},
{
present: String.raw`\(`,
"index": 1,
text: "You do not need parentheses to solve this problem."
},
{
absent: String.raw`(\[A-Z\]\+|
\[A-Z\]\[A-Z\]\*)`,
"index": 1,
text: "You can use [A-Z]+ to match one or more uppercase Latin letters.",
"examples": [
[ "^[YN]$", "^[A-Z]$" ]
]
},
{
present: "True",
"index": 2,
text: "Regular expressions are case-sensitive by default; use \"true\"."
},
{
present: "False",
"index": 2,
text: "Regular expressions are case-sensitive by default; use \"false\"."
},
{
absent: String.raw`\|`,
"index": 2,
text: "Use \"|\" to express alternatives."
},
{
present: String.raw`^\^true\|false\$$`,
"index": 2,
text: "No. This would match anything beginning with the term true, or anything ending with the term false. Use parenthesis."
},
{
present: String.raw`^\^false\|true\$$`,
"index": 2,
text: "No. This would match anything beginning with the term false, or anything ending with the term true. Use parenthesis."
},
{
absent: String.raw`\(`,
"index": 2,
text: "Use parentheses."
},
{
present: String.raw`\$`,
"index": 3,
text: "This is Python, not ECMAScript (JavaScript). Use \\Z at the end, not $."
},
{
present: String.raw`\\z`,
"index": 3,
text: "This is Python. Use \\Z at the end, not \\z."
},
{
absent: String.raw`^\\A`,
"index": 4,
text: "This is Ruby. Use \\A at the beginning."
},
{
absent: String.raw`\\z$`,
"index": 4,
text: "This is Ruby. Use \\z at the end."
},
{
absent: String.raw`\[A-Z\]`,
"index": 4,
text: "Use [A-Z] to match one uppercase Latin letter."
},
{
present: String.raw`\[A-Z\](\*|\+)`,
"index": 4,
text: "In this case we are only matching one letter, not many of them. Do not use \"*\" or \"+\" after [A-Z]."
}
],
preprocessing: [
[
"\\s*",
""
]
]
}