MCPcopy
hub / github.com/BloombergGraphics/whatiscode / testRegExp

Function testRegExp

scripts/libs/esprima.js:1084–1125  ·  view source on GitHub ↗
(pattern, flags)

Source from the content-addressed store, hash-verified

1082 }
1083
1084 function testRegExp(pattern, flags) {
1085 var tmp = pattern;
1086
1087 if (flags.indexOf('u') >= 0) {
1088 // Replace each astral symbol and every Unicode escape sequence
1089 // that possibly represents an astral symbol or a paired surrogate
1090 // with a single ASCII symbol to avoid throwing on regular
1091 // expressions that are only valid in combination with the `/u`
1092 // flag.
1093 // Note: replacing with the ASCII symbol `x` might cause false
1094 // negatives in unlikely scenarios. For example, `[\u{61}-b]` is a
1095 // perfectly valid pattern that is equivalent to `[a-b]`, but it
1096 // would be replaced by `[x-b]` which throws an error.
1097 tmp = tmp
1098 .replace(/\\u\{([0-9a-fA-F]+)\}/g, function ($0, $1) {
1099 if (parseInt($1, 16) <= 0x10FFFF) {
1100 return 'x';
1101 }
1102 throwUnexpectedToken(null, Messages.InvalidRegExp);
1103 })
1104 .replace(
1105 /\\u([a-fA-F0-9]{4})|[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
1106 'x'
1107 );
1108 }
1109
1110 // First, detect invalid regular expressions.
1111 try {
1112 RegExp(tmp);
1113 } catch (e) {
1114 throwUnexpectedToken(null, Messages.InvalidRegExp);
1115 }
1116
1117 // Return a regular expression object for this pattern-flag pair, or
1118 // `null` in case the current environment doesn't support the flags it
1119 // uses.
1120 try {
1121 return new RegExp(pattern, flags);
1122 } catch (exception) {
1123 return null;
1124 }
1125 }
1126
1127 function scanRegExpBody() {
1128 var ch, str, classMarker, terminated, body;

Callers 1

scanRegExpFunction · 0.85

Calls 1

throwUnexpectedTokenFunction · 0.85

Tested by

no test coverage detected