2 回答

TA貢獻1786條經驗 獲得超11個贊
您應該能夠使用積極的前瞻來斷言在您的經期之后出現空格或 EOL。
\.(?=\s|$)
\.
匹配文字句點(?=...)
是一個積極的前瞻,它將斷言在您的經期之后出現一個空格\s
或 EOL$
。在 URL 中永遠不會出現這種情況,這就是它起作用的原因。
如果你想減少限制,你可以用任何非字母數字字符代替空格替換你的前瞻。這是一個替代方案:
/\.(?=[^a-z0-9]|$)/igm

TA貢獻1804條經驗 獲得超7個贊
您可以使用以下表達式: ([\w]+:\/\/[^\s]+)([\.](?=\s|$))
將 url 減去句點裝飾為降價超鏈接的示例:
const expression = /([\w]+:\/\/[^\s]+)([\.](?=\s|$))/g;
const input = `http://stackoverflow.com.
In this snippet Here is some sample text with a URL: http://stackoverflow.com. I would prefer to select periods outside of URLs though.
http://stackoverflow.com`;
const output = input.replace(expression, (match, url, period) => `[${url}](${url})${period}`);
console.log(output);
添加回答
舉報