[英]Regex - Find all unique strings excluding some patterns
I would like to find all unique strings in my JavaScript project. 我想在我的JavaScript项目中找到所有唯一的字符串。 I'm using WebStorm to search for it and I have this in terms of my search:
我正在使用WebStorm进行搜索,而我的搜索条件如下:
'.*' // any number of quoted characters
This does get me all the strings in the project but I would like to exclude some and am not sure how to. 这确实使我获得了项目中的所有字符串,但是我想排除一些字符串,但不确定如何做。
I get results like import
s and require
s which I would like to exclude: 我得到类似
import
的结果,并require
我要排除的结果:
const _ = require('lodash');
and 和
import React from 'react';
What could I enter for a search to exclude that from my results? 我可以输入什么来将搜索结果排除在搜索范围之外?
尝试这样的否定前瞻(?!require)(?!import)('.*')
,它似乎产生了我想要的结果
I don't think the regex you posted in your answer (?!require)(?!import)('.*')
works: 我认为您在答案
(?!require)(?!import)('.*')
发布的正则表达式不起作用:
const tests = [ "const _ = require('lodash');", "import React from 'react';", "some other 'string'" ]; tests.forEach(test => { console.log(/(?!require)(?!import)('.*')/.test(test)); });
Negative lookbehind works: 负向后看的作品:
const tests = [ "const _ = require('lodash');", "import React from 'react';", "some other 'string'" ]; tests.forEach(test => { console.log(/(?<!(require|import).*)'.+'/.test(test)); });
Caveat: I believe negative lookbehind is currently only supported in Chrome 警告:我认为目前仅Chrome支持负向后看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.