MCPcopy
hub / github.com/aykutkardas/regexlearn.com / checkRegex

Function checkRegex

src/components/PlaygroundEditor.tsx:95–148  ·  view source on GitHub ↗
(regex, flags, editorState)

Source from the content-addressed store, hash-verified

93 };
94
95 const checkRegex = (regex, flags, editorState) => {
96 let rowIndex = 0;
97 let matchCount = 0;
98
99 if (!regex) {
100 const content = editorState.getCurrentContent();
101 return EditorState.createWithContent(content);
102 }
103
104 function findWithRegex(content: ContentBlock, callback: Function) {
105 const isMultiple = flags.includes('m');
106 const isNeededMultiple = regex.startsWith('^') || regex.endsWith('$');
107
108 if (!isMultiple && isNeededMultiple && rowIndex > 0) return;
109
110 const isGlobal = flags.includes('g');
111
112 if (!isGlobal && matchCount > 0) return;
113
114 const text = content.getText();
115 const currentRegex = new RegExp(regex, isGlobal ? flags : `g${flags}`);
116
117 let matches = [...text.matchAll(currentRegex)];
118
119 if (!isGlobal) {
120 matches = matches.slice(0, 1);
121 }
122
123 if (regex && matches.length) {
124 matches.forEach(match => callback(match.index, match.index + match[0].length));
125 }
126
127 if (matches.length) {
128 matchCount++;
129 }
130
131 rowIndex++;
132 }
133
134 function handleStrategy(content: ContentBlock, callback: Function) {
135 try {
136 findWithRegex(content, callback);
137 } catch (err) {}
138 }
139
140 const HighlightDecorator = new CompositeDecorator([
141 {
142 strategy: handleStrategy,
143 component: Highlight,
144 },
145 ]);
146
147 return EditorState.createWithContent(editorState.getCurrentContent(), HighlightDecorator);
148 };
149
150 const handleShare = () => {
151 axios

Callers 6

applyRegexFunction · 0.70
InteractiveAreaFunction · 0.70
applyRegexFunction · 0.70
onChangeFlagsFunction · 0.70
onChangeRegexFunction · 0.70
PlaygroundFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected