Skip to content

Commit

Permalink
Don't attempt to negate octal sequences that are not wrapped in chara…
Browse files Browse the repository at this point in the history
…cter classes
  • Loading branch information
hlysine committed Aug 18, 2023
1 parent c10b385 commit 9a221b9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ export const hexNumber = /^[0-9a-fA-F]+$/;

export const octalNumber = /^[0-7]+$/;

export const charLiteral = /^(?:\\u[0-9a-fA-F]{4}$|\\x[0-9a-fA-F]{2}|\\\d{1,3})/; // last option refers to octal character or capture group backreference
// octal escape sequences are not matched here because they should be wrapped in a character class
export const negatableCharLiteral = /^(?:\\u[0-9a-fA-F]{4}|\\x[0-9a-fA-F]{2})$/;

// last option refers to octal character or capture group backreference
export const charLiteral = /^(?:\\u[0-9a-fA-F]{4}|\\x[0-9a-fA-F]{2}|\\\d{1,3})$/;

export const captureName = /^[a-zA-Z_][a-zA-Z0-9_]*$/;

Expand Down
4 changes: 2 additions & 2 deletions src/modifiers/NegationModifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RegExpModifier } from '../types';
import { charLiteral, isBracketGroup, isCharacterClass, negatableTokens } from '../helper';
import { isBracketGroup, isCharacterClass, negatableCharLiteral, negatableTokens } from '../helper';

export default class NegationModifier implements RegExpModifier {
public modify(regExp: string): [string, string?] {
Expand All @@ -15,7 +15,7 @@ export default class NegationModifier implements RegExpModifier {
case '$':
return [`(?!${regExp})`];
default:
if (charLiteral.test(regExp)) {
if (negatableCharLiteral.test(regExp)) {
return [`[^${regExp}]`];
} else if (negatableTokens.test(regExp)) {
return [regExp.toUpperCase()];
Expand Down

0 comments on commit 9a221b9

Please sign in to comment.