Liu Song’s Projects


~/Projects/scripts

git clone https://code.lsong.org/scripts

Commit

Commit
3bbd44ebbabbe2196c12489c020508d6cef23b24
Author
Lsong <[email protected]>
Date
2021-01-07 17:10:32 +0800 +0800
Diffstat
 example/index.html | 1 
 example/regexp/index.html | 12 +++++++
 example/regexp/index.js | 2 +
 regexp.js | 69 +++++++++++++++++++++++++++++++++++++++++

add regexp


diff --git a/example/index.html b/example/index.html
index a6ebf4e9eda5b61e30b9dae6cfaf2f11bb0c1121..7c6615328873a782c5360be54172c1ebae07c7d0 100644
--- a/example/index.html
+++ b/example/index.html
@@ -11,6 +11,7 @@ 
   <h1>Playground</h1>
   <ul>
     <li><a href="./dom">DOM</a></li>
+    <li><a href="./regexp">RegExp</a></li>
     <li><a href="./router">Router</a></li>
     <li><a href="./file">File API</a></li>
     <li><a href="./datetime">DateTime</a></li>




diff --git a/example/regexp/index.html b/example/regexp/index.html
new file mode 100644
index 0000000000000000000000000000000000000000..cefa259275505a96a0d235f185da11bef32ba28b
--- /dev/null
+++ b/example/regexp/index.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>RegExp</title>
+  <script type="module" src="./index.js"></script>
+</head>
+<body>
+  
+</body>
+</html>




diff --git a/example/regexp/index.js b/example/regexp/index.js
new file mode 100644
index 0000000000000000000000000000000000000000..992bd0681b078367090565bd2db158d847a79ef1
--- /dev/null
+++ b/example/regexp/index.js
@@ -0,0 +1,2 @@
+import { search } from '../../regexp.js';
+




diff --git a/regexp.js b/regexp.js
new file mode 100644
index 0000000000000000000000000000000000000000..27e5b1ebabb8e668cfde79c760f3d2c2b9b54e60
--- /dev/null
+++ b/regexp.js
@@ -0,0 +1,69 @@
+//A walkthrough of this solution exists here: https://nickdrane.com/build-your-own-regex/
+
+export function matchOne(pattern, text) {
+  if (!pattern) return true;
+  if (!text) return false;
+  return pattern === "." || text === pattern;
+}
+
+export function search(pattern, text) {
+  if (pattern[0] === "^") {
+    return match(pattern.slice(1), text);
+  } else {
+    return match(".*" + pattern, text);
+  }
+}
+
+export function match(pattern, text) {
+  if (!pattern) return true;
+  else if (!text && pattern === "$") return true;
+  else if (pattern[1] === "?") {
+    return matchQuestion(pattern, text);
+  } else if (pattern[1] === "*") {
+    return matchStar(pattern, text);
+  } else if (pattern[0] === "(") {
+    return matchGroup(pattern, text);
+  } else {
+    return matchOne(pattern[0], text[0]) && match(pattern.slice(1), text.slice(1));
+  }
+}
+
+export function matchQuestion(pattern, text) {
+  return (
+    (matchOne(pattern[0], text[0]) && match(pattern.slice(2), text.slice(1))) ||
+    match(pattern.slice(2), text)
+  );
+}
+
+export function matchStar(pattern, text) {
+  return (
+    (matchOne(pattern[0], text[0]) && match(pattern, text.slice(1))) ||
+    match(pattern.slice(2), text)
+  );
+}
+
+export function matchGroup(pattern, text) {
+  const groupEnd = pattern.indexOf(")");
+  const groupPattern = pattern.slice(1, groupEnd);
+  if (pattern[groupEnd + 1] === "?") {
+    const remainderPattern = pattern.slice(groupEnd + 2); // +2 needed to slice off the ')?'
+    return (
+      (match(groupPattern, text.slice(0, groupPattern.length)) &&
+        match(remainderPattern, text.slice(groupPattern.length))) ||
+      match(remainderPattern, text)
+    );
+  } else if (pattern[groupEnd + 1] === "*") {
+    const remainderPattern = pattern.slice(groupEnd + 2); // +2 needed to slice off the ')*'
+    return (
+      (match(groupPattern, text.slice(0, groupPattern.length)) &&
+        match(pattern, text.slice(groupPattern.length))) ||
+      match(remainderPattern, text)
+    );
+  } else {
+    const remainderPattern = pattern.slice(groupEnd + 1); // +1 needed to slice off the ')'
+    return (
+      match(groupPattern, text.slice(0, groupPattern.length)) &&
+      match(remainderPattern, text.slice(groupPattern.length))
+    );
+  }
+}