mirror of
https://github.com/wassname/stampy-chat.git
synced 2026-09-10 12:40:44 +08:00
add word boundaries to terms, escape regex chars
This commit is contained in:
@@ -15,8 +15,11 @@ const MyApp: AppType = ({ Component, pageProps }) => {
|
||||
.then((res) => res.json())
|
||||
.then((data) => {
|
||||
const glossary: Glossary = new Map(Object.entries(data));
|
||||
const keys = Array.from(glossary.keys()).sort((a, b) => b.length - a.length);
|
||||
console.log(keys);
|
||||
const keys = Array.from(glossary.keys())
|
||||
.sort((a, b) => b.length - a.length) // sort by length descending
|
||||
.map((k) => k.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')) // escape regex chars
|
||||
.map((k) => `\\b${k}\\b`); // add word boundaries
|
||||
|
||||
const regex = new RegExp(keys.join("|"), "gim");
|
||||
setGlossary({ g: glossary, r: regex });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user