假设我们有以下导入语句(非特定语言):
import "./test"
import "./test" as Test
import { Test } from "./test"
import { Person as Individual } from "./test"
我正在尝试编写一个正则表达式来检索以下粗体组:
导入 "./test"
导入 "./test"作为测试
导入 { 测试} 来自 "./test"
import { Person as Individual, Test, Bag as Backpack } from "./test"
我尝试编写以下正则表达式:
/^(import)(?:.+?)(\s+from\s+|\s+as\s+)*(?:.+?)$/gm
https://regex101.com/r/4sO59R/1
但它并没有像我期望的那样工作......我只能得到导入...... 应该怎么写?
最佳答案
虽然用于捕获数据的理想正则表达式似乎很难,但我能想到的最接近的是给定文本,
^(import)(?:.*?(as))?(?:.*?(as))?(?:.*?(from))*.*$
关于javascript - 用于在导入语句中查找关键字的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52661076/