字符串使用具有特殊条件的正则表达式替换哈希(先行/后行)
String replace Hash using regex with special condition (lookahead / lookbehind)
让我们留下来我有以下字符串列表,在 JavaScript (NodeJS) 我想一次应用一个正则表达式并替换 .
和 .
之间的散列,但我不想替换 .style
这个词。或 .bundle.
或 .chunk.
并且在没有前面的点的情况下它也应该可以替换散列。
// 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
结果应该是
// 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
我努力了,最接近的是这个
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;
});
但这只会替换以数字开头的哈希值,而忽略所有其余部分。
我也试过 /\.(?!(?:[A-Za-z]))(([^.]*))\./gi;
但这只是替换了第一次出现的 .something.
我也没有遇到负面回顾(根据 here and here,这是 JS 中的一个新功能)
正确的正则表达式是什么?
这个怎么样?
[^.]+\.(?=[^.]+$)
它查找后跟句点 ([^.]+\.
) 的任何非句点字符,然后是(使用先行)一系列非句点字符 ([^.]+
) 和结尾-行 ($
).
让我们留下来我有以下字符串列表,在 JavaScript (NodeJS) 我想一次应用一个正则表达式并替换 .
和 .
之间的散列,但我不想替换 .style
这个词。或 .bundle.
或 .chunk.
并且在没有前面的点的情况下它也应该可以替换散列。
// 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
结果应该是
// 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
我努力了,最接近的是这个
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;
});
但这只会替换以数字开头的哈希值,而忽略所有其余部分。
我也试过 /\.(?!(?:[A-Za-z]))(([^.]*))\./gi;
但这只是替换了第一次出现的 .something.
我也没有遇到负面回顾(根据 here and here,这是 JS 中的一个新功能)
正确的正则表达式是什么?
这个怎么样?
[^.]+\.(?=[^.]+$)
它查找后跟句点 ([^.]+\.
) 的任何非句点字符,然后是(使用先行)一系列非句点字符 ([^.]+
) 和结尾-行 ($
).