(str)
| 4159 | |
| 4160 | // Translates a search string from ex (vim) syntax into javascript form. |
| 4161 | function translateRegex(str) { |
| 4162 | // When these match, add a '\' if unescaped or remove one if escaped. |
| 4163 | var specials = '|(){'; |
| 4164 | // Remove, but never add, a '\' for these. |
| 4165 | var unescape = '}'; |
| 4166 | var escapeNextChar = false; |
| 4167 | var out = []; |
| 4168 | for (var i = -1; i < str.length; i++) { |
| 4169 | var c = str.charAt(i) || ''; |
| 4170 | var n = str.charAt(i+1) || ''; |
| 4171 | var specialComesNext = (n && specials.indexOf(n) != -1); |
| 4172 | if (escapeNextChar) { |
| 4173 | if (c !== '\\' || !specialComesNext) { |
| 4174 | out.push(c); |
| 4175 | } |
| 4176 | escapeNextChar = false; |
| 4177 | } else { |
| 4178 | if (c === '\\') { |
| 4179 | escapeNextChar = true; |
| 4180 | // Treat the unescape list as special for removing, but not adding '\'. |
| 4181 | if (n && unescape.indexOf(n) != -1) { |
| 4182 | specialComesNext = true; |
| 4183 | } |
| 4184 | // Not passing this test means removing a '\'. |
| 4185 | if (!specialComesNext || n === '\\') { |
| 4186 | out.push(c); |
| 4187 | } |
| 4188 | } else { |
| 4189 | out.push(c); |
| 4190 | if (specialComesNext && n !== '\\') { |
| 4191 | out.push('\\'); |
| 4192 | } |
| 4193 | } |
| 4194 | } |
| 4195 | } |
| 4196 | return out.join(''); |
| 4197 | } |
| 4198 | |
| 4199 | // Translates the replace part of a search and replace from ex (vim) syntax into |
| 4200 | // javascript form. Similar to translateRegex, but additionally fixes back references |
no test coverage detected