1

I have two sample strings of code like so:

"const x = anything;"
"const {a, b, c, d} = anything;"

I want to match x, as well as a, b, c, and d (none of which are literal, they can be any valid JavaScript variable character(s), but are delimited by commas) if they are preceded by the keyword const. The latter is the destructuring syntax.

Context: this is to extend syntax highlighting of code tokens in PrismJS.

What I've tried so far:

  Prism.languages.insertBefore(lang, 'operator', {
    constant: {
      pattern: /(const\s?{?)[\s_$a-zA-Z\xA0-\uFFFF,]*(?!:)/,
      lookbehind: true,
    },
  });

The comma is inside the [], and I am not sure of how to "continue" matching each variable inside the braces without including the comma character.

4

1 回答 1

0

试试这个模式:

(?:const\s+{?\s*)[_$\w]+(?:[,}\s])

\w我将尝试通过使用非字母数字字符替换以匹配标识符来改进它。可能:

(?:const\s+{?\s*)[_$\p{L}]+(?:[,}\s])
于 2021-12-04T15:22:34.950 回答