MCPcopy Index your code
hub / github.com/witheve/Eve / translateRegex

Function translateRegex

src/codemirror.js:12977–13013  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

12975
12976 // Translates a search string from ex (vim) syntax into javascript form.
12977 function translateRegex(str) {
12978 // When these match, add a '\' if unescaped or remove one if escaped.
12979 var specials = '|(){';
12980 // Remove, but never add, a '\' for these.
12981 var unescape = '}';
12982 var escapeNextChar = false;
12983 var out = [];
12984 for (var i = -1; i < str.length; i++) {
12985 var c = str.charAt(i) || '';
12986 var n = str.charAt(i+1) || '';
12987 var specialComesNext = (n && specials.indexOf(n) != -1);
12988 if (escapeNextChar) {
12989 if (c !== '\\' || !specialComesNext) {
12990 out.push(c);
12991 }
12992 escapeNextChar = false;
12993 } else {
12994 if (c === '\\') {
12995 escapeNextChar = true;
12996 // Treat the unescape list as special for removing, but not adding '\'.
12997 if (n && unescape.indexOf(n) != -1) {
12998 specialComesNext = true;
12999 }
13000 // Not passing this test means removing a '\'.
13001 if (!specialComesNext || n === '\\') {
13002 out.push(c);
13003 }
13004 } else {
13005 out.push(c);
13006 if (specialComesNext && n !== '\\') {
13007 out.push('\\');
13008 }
13009 }
13010 }
13011 }
13012 return out.join('');
13013 }
13014
13015 // Translates the replace part of a search and replace from ex (vim) syntax into
13016 // javascript form. Similar to translateRegex, but additionally fixes back references

Callers 1

parseQueryFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected