英文:
regex to use negative look far behind along with positive look behind?
问题
I need it to select only value which has :this
in line but not include which are under comment section (/* */)
我需要选择只包含 :this
的值,但不包括在注释部分(/* */)的值。
I have tried this one which works and give 3 results but it is incorrect as it select the last one which is in comment
我尝试过这个方法,它可以工作并返回3个结果,但不正确,因为它选择了在注释中的最后一个值。
Trial 1:
look far behind that is there *
in the same line but it does not work and gives 0 result
尝试1:
查找在同一行中是否有 *
,但它不起作用,返回0个结果。
英文:
I need it to select only value which has :this
in line but not include which are under comment section (/* */)
I have tried this one which works and give 3 results but it is incorrect as it select the last one which is in comment
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const str = `
:this {
display: grid;
}
p { color: red}
:this { this is also okay to capture }
/* but do not capture this :this { } , so how to look far behind here */
`;
const pattren = '(?<=:)\\bthis\\b\\s*(?={)';
const rx = new RegExp(pattren, 'gmd');
const matches = str.matchAll(rx);
console.log([...matches]);
<!-- end snippet -->
Trial 1:
look far behind that is there *
in the same line but it does not work and gives 0 result
const pattern = '(?<!/*)(?<=:)\\bthis\\b\\s*(?={)';
答案1
得分: 1
你可以使用 (?<!/\*.*)
来查找 /*
后面跟着零个或多个字符。
const str = `
:this {
display: grid;
}
p { color: red}
:this { this is also okay to capture }
/* but do not capture this :this { } , so how to look far behind here */
`;
const re = /(?<!\/\*.*)(?<=:)\bthis\b\s*(?={)/gmd;
const matches = str.matchAll(re);
console.log([...matches]);
请注意,这是你提供的代码的一部分,仅翻译了代码内容。
英文:
You can use (?<!/\*.*)
to look for /*
followed by zero or more characters.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const str = `
:this {
display: grid;
}
p { color: red}
:this { this is also okay to capture }
/* but do not capture this :this { } , so how to look far behind here */
`;
const re = /(?<!\/\*.*)(?<=:)\bthis\b\s*(?={)/gmd;
const matches = str.matchAll(re);
console.log([...matches]);
<!-- end snippet -->
答案2
得分: 1
我会匹配注释块,使其不影响正则匹配。使用捕获组,您可以识别要保留的内容,并丢弃捕获组中没有内容的匹配项。
在这个代码片段中,输出包含匹配项的起始和结束索引:
const str = `
:this {
display: grid;
}
p { color: red}
:this { this is also okay to capture }
/* but do not capture this :this { } , so how to look far behind here */
`;
const rx = /\/\*.*?\*\/|:(this\b\s*)\{/gsd;
const matches = Array.from(str.matchAll(rx), m => m.indices?.[1]).filter(Boolean);
console.log(matches);
请注意,我没有翻译代码部分,只提供了代码的解释。
英文:
I would match the comment block, so it is out of the way. With capture groups you can then identify what you want to keep, and throw away the matches that don't have anything in the capture group.
In this snippet the output has the start-end indices of the matches:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const str = `
:this {
display: grid;
}
p { color: red}
:this { this is also okay to capture }
/* but do not capture this :this { } , so how to look far behind here */
`;
const rx = /\/\*.*?\*\/|:(this\b\s*)\{/gsd;
const matches = Array.from(str.matchAll(rx), m => m.indices?.[1]).filter(Boolean);
console.log(matches);
<!-- end snippet -->
答案3
得分: 0
I doubt there is a way achieving what the OP is wishing for with a positive lookbehind but a negative lookbehind like ...
/(?<!\/\*.*?):this\s*\{[^}]+\}/g
... which gets explained at its playground page does the job.
英文:
I doubt there is a way achieving what the OP is wishing for with a positive lookbehind but a negative lookbehind like ...
/(?<!\/\*.*?):this\s*\{[^}]+\}/g
... which gets explained at its playground page does the job.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// see ... [https://regex101.com/r/0p8Uw2/1]
const regXExtractUncommentedThisRule = /(?<!\/\*.*?):this\s*\{[^}]+\}/g;
const sampleData = `
:this {
display: grid;
}
p { color: red}
:this { this is also okay to capture }
/* but do not capture this :this { } , so how to look far behind here */
:this {
display: grid;
}
p { color: red}
:this { this is also okay to capture }
/* but do not capture this :this { } , so how to look far behind here */`;
console.log(
sampleData.match(regXExtractUncommentedThisRule)
);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论