Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

webpack - Regex to catch text not containing a string and ending with an extension

I'm trying to have 2 regex, one catching string containing 'critrical' and ending with '(s)css' and second one for string which don't contain 'critical' (with the same extentions rule).

I already have the first one and it is /.*?critical.*.s?css$/

But I can't manage to make the second one. I thought it would be as easy as /.*?(?<!.critical).*.s?css$/. But, seems it's not that easy as it doesn't work...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this regex(for the string not containing the word critical and containing css in the end):

^(?!.*critical).*s?css$

Click for Demo

Explanation:

  • ^ - asserts the start of the string
  • (?!.*critical) - negative lookahead to validate that the word critical is not present in the string
  • .* - matches 0+ occurrences(greedily) of any character except a new line
  • s?css - matches an optional s followed by css
  • $ - asserts the end of the string

And, for the string containing the word critical and containing css in the end, you can try:

^(?=.*critical).*s?css$

Click for Demo

The only difference with this regex is that it uses a Positive lookahead to make sure that the string critical exist somewhere in the string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...