Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Automatically clean token cruft.

  • Loading branch information...
commit 6b1da538d96f37de7a95293e2353673e83812e2c 1 parent bde574a
@slevithan authored
Showing with 18 additions and 12 deletions.
  1. +6 −4 src/addons/build.js
  2. +3 −2 src/xregexp.js
  3. +9 −6 xregexp-all.js
View
10 src/addons/build.js
@@ -19,12 +19,14 @@
* @returns {String} Pattern with edge anchors removed.
*/
function deanchor(pattern) {
- var startAnchor = /^(?:\(\?:\))*\^/, // Leading `^` or `(?:)^` (handles token cruft)
- endAnchor = /\$(?:\(\?:\))*$/; // Trailing `$` or `$(?:)` (handles token cruft)
+ var leadingAnchor = /^\^/,
+ trailingAnchor = /\$$/;
+
// Ensure that the trailing `$` isn't escaped
- if (startAnchor.test(pattern) && endAnchor.test(pattern.replace(/\\[\s\S]/g, ''))) {
- return pattern.replace(startAnchor, '').replace(endAnchor, '');
+ if (leadingAnchor.test(pattern) && trailingAnchor.test(pattern.replace(/\\[\s\S]/g, ''))) {
+ return pattern.replace(leadingAnchor, '').replace(trailingAnchor, '');
}
+
return pattern;
}
View
5 src/xregexp.js
@@ -474,7 +474,8 @@ var XRegExp = (function(undefined) {
}
patternCache[key] = {
- pattern: output,
+ // Cleanup token cruft: repeated `(?:)(?:)` and leading/trailing `(?:)`
+ pattern: nativ.replace.call(output, /\(\?:\)(?=\(\?:\))|^\(\?:\)|\(\?:\)$/g, ''),
// Strip all but native flags
flags: nativ.replace.call(flags, /[^gimy]+/g, ''),
// `context.captureNames` has an item for each capturing group, even if unnamed
@@ -1521,7 +1522,7 @@ var XRegExp = (function(undefined) {
/* Whitespace and line comments, in free-spacing mode (aka extended mode, flag x) only.
*/
add(
- /(?:\s+|#.*)+/,
+ /\s+|#.*/,
function(match) {
// Keep tokens separated unless the following token is a quantifier
return nativ.test.call(quantifier, match.input.slice(match.index + match[0].length)) ?
View
15 xregexp-all.js
@@ -501,7 +501,8 @@ var XRegExp = (function(undefined) {
}
patternCache[key] = {
- pattern: output,
+ // Cleanup token cruft: repeated `(?:)(?:)` and leading/trailing `(?:)`
+ pattern: nativ.replace.call(output, /\(\?:\)(?=\(\?:\))|^\(\?:\)|\(\?:\)$/g, ''),
// Strip all but native flags
flags: nativ.replace.call(flags, /[^gimy]+/g, ''),
// `context.captureNames` has an item for each capturing group, even if unnamed
@@ -1548,7 +1549,7 @@ var XRegExp = (function(undefined) {
/* Whitespace and line comments, in free-spacing mode (aka extended mode, flag x) only.
*/
add(
- /(?:\s+|#.*)+/,
+ /\s+|#.*/,
function(match) {
// Keep tokens separated unless the following token is a quantifier
return nativ.test.call(quantifier, match.input.slice(match.index + match[0].length)) ?
@@ -1680,12 +1681,14 @@ var XRegExp = (function(undefined) {
* @returns {String} Pattern with edge anchors removed.
*/
function deanchor(pattern) {
- var startAnchor = /^(?:\(\?:\))*\^/, // Leading `^` or `(?:)^` (handles token cruft)
- endAnchor = /\$(?:\(\?:\))*$/; // Trailing `$` or `$(?:)` (handles token cruft)
+ var leadingAnchor = /^\^/,
+ trailingAnchor = /\$$/;
+
// Ensure that the trailing `$` isn't escaped
- if (startAnchor.test(pattern) && endAnchor.test(pattern.replace(/\\[\s\S]/g, ''))) {
- return pattern.replace(startAnchor, '').replace(endAnchor, '');
+ if (leadingAnchor.test(pattern) && trailingAnchor.test(pattern.replace(/\\[\s\S]/g, ''))) {
+ return pattern.replace(leadingAnchor, '').replace(trailingAnchor, '');
}
+
return pattern;
}
Please sign in to comment.
Something went wrong with that request. Please try again.