簡體   English   中英

正則表達式-捕獲標記的模板文字

[英]Regex - capture tagged template literal

我需要構建正則表達式的幫助,以在js文件中找到所有標記的模板文字

例:

const thing = test`
  background-color: red;
`;

正則表達式結果:

test`
  background-color: red;
`

我可以使用以下方法完成此操作:

(test`(?:[^`])*`)

問題是我不知道如何排除內部模板文字。

例如:

const thing = test`
  background-color: ${show ? `red` : `blue`};
`;

預期的正則表達式匹配項:

test`
  background-color: ${show ? `red` : `blue`};
`

實際匹配:

test`
  background-color: ${show ? `

有任何想法嗎?

正則表達式不會幫助你在這里,這是最好的解析JS文件與AST解析器像@babel/parser - https://babeljs.io/docs/en/next/babel-parser.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM