简体   繁体   English

使用特殊条件的正则表达式将字符串替换为哈希(超前/后退)

[英]String replace Hash using regex with special condition (lookahead / lookbehind)

Lets stay I have the following list of string, in JavaScript (NodeJS) I want to apply a regex one at a time and replace the hash between . 让我们留下以下字符串列表,在JavaScript(NodeJS)中,我想一次应用一个正则表达式并替换之间的哈希. and . . , but I don't want to replace the word, .style . ,但我不想替换.style这个词。 or .bundle. .bundle. or . 要么 。 chunk. and it should also work to replace the hash in the case where there is no preceding dots. 并且在没有前面的点的情况下,它也应该替换哈希。

// hash starts with number
/css/app.style.7fef363d5a8c4ef2458c.css
/css/app.bundle.7fef363d5a8c4ef2458c.css
/css/app.chunk.7fef363d5a8c4ef2458c.css
/css/app.7fef363d5a8c4ef2458c.css
/css/loading-animation.7fef363d5a8c4ef2458c.css

// hash starts with letter
/css/app.style.b3bcb606396f0c96623a.css
/css/app.bundle.b3bcb606396f0c96623a.css
/css/app.chunk.b3bcb606396f0c96623a.css
/css/app.b3bcb606396f0c96623a.css

// no preceeding dot separated name before the hash
/css/loading-animation.b3bcb606396f0c96623a.css
/css/app.b3bcb606396f0c96623a.js

// It should not affect items without a hash
/fonts/MaterialIcons-Regular.eot
/fonts/MaterialIcons-Regular.svg

The result should be 结果应该是

// hash starts with number
/css/app.style.css
/css/app.bundle.css
/css/app.chunk.css
/css/app.css
/css/loading-animation.css

// hash starts with letter
/css/app.style.css
/css/app.bundle.css
/css/app.chunk.css
/css/app.css

// no preceeding dot separated name before the hash
/css/loading-animation.css
/css/app.js

// It should not affect items without a hash
/fonts/MaterialIcons-Regular.eot
/fonts/MaterialIcons-Regular.svg

I tried hard and the closest I got was this 我努力了,最接近的是这个

 const manifest = {};
 stats.assets.map(asset => asset.name)
    .sort()
    .forEach((file) => {
       let regEx = /\.(?!(?:[A-Za-z]))(([^.]*))\./gi
       let passTest = regEx.test(file);
       let key = file.replace(regEx, '.');
       console.log(file, passTest, key);
       manifest[key] = file;
    });

But that only replaces the hash starting with the number, and ignores all the rest. 但这只会替换以数字开头的哈希,而忽略其余所有哈希。

I also tried /\\.(?!(?:[A-Za-z]))(([^.]*))\\./gi; 我也尝试过/\\.(?!(?:[A-Za-z]))(([^.]*))\\./gi; but that just replaced the first occurrence of .something. 但这只是替换了.something.的第一次出现.something.

I also had no luck with negative lookbehinds (it is a new feature in JS according to here and here ) 我也没有运气带有负面的回望(根据此处此处,这是JS中的新功能)

What is the proper regex for this? 正确的正则表达式是什么?

How about this? 这个怎么样?

[^.]+\.(?=[^.]+$)

It looks for any non-period characters followed by a period ( [^.]+\\. ) followed by (using a lookahead) a series of non-period characters ( [^.]+ ) and an end-of-line ( $ ). 它查找任何非句点字符,后跟一个句点( [^.]+\\. ),然后是(使用超前方式)一系列非句点字符( [^.]+ )和一个行尾( $ )。

https://regex101.com/r/5QfED2/1 https://regex101.com/r/5QfED2/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM