@samitouri / QOS-React-2 / commits / 86b44349de

Avoid negative lookahead in regular expression

I'm trying to use React with otto (https://github.com/robertkrimen/otto), which is implemented in Go and does not support lookaheads or backreferences in regular expressions. (Unfortunately that *does* mean that otto isn't fully compatible with the ECMA5 spec; but since this is the only lookahead I am aware of in the React codebase, this change makes React once again compatible with otto, and any other JS implementation that may not be fully compliant on regular expressions). As far as I can tell, the previous code replaced a sequence of slash characters with one more slash than before. The new code avoids the negative lookahead by matching any sequence of slash characters and then using the special `$&` placeholder to replace the matched sequence with the original sequence, plus one more slash.

Mark Rushakoff committed Dec 6, 2015 at 13:46 UTC 86b44349dec8db499935927260821cdee211ece4
1 file changed +2 -2
src/isomorphic/children/ReactChildren.js
+2 -2
@@ -21,9 +21,9 @@ var twoArgumentPooler = PooledClass.twoArgumentPooler;
21 var fourArgumentPooler = PooledClass.fourArgumentPooler;
22
23
24 -var userProvidedKeyEscapeRegex = /\/(?!\/)/g;
24 +var userProvidedKeyEscapeRegex = /\/+/g;
25 function escapeUserProvidedKey(text) {
26 - return ('' + text).replace(userProvidedKeyEscapeRegex, '//');
26 + return ('' + text).replace(userProvidedKeyEscapeRegex, '$&/');
27 }
28
29