const regex = /(?<!(i|e))[.,!?][a-zA-Z0-9]/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<!(i|e))[.,!?][a-zA-Z0-9]', 'gm')
const str = `The problem:
Match lack of space following a fullstop/period. BUT! don't match on "i.e." and "e.g."
The current attempt above is not robust, only matches the "i" of "i.e." and "e" of "e.g." and, worse, allows all other sentences ending with "i" or "e".
Example source:
PASS: This is a test (i.e. something).Match M with no space. Don't match i.e.
PASS: This is a test (e.g. Something).Match M with no space. Don't match e.g.
PASS: This is a test (q.v. something).Match M with no space. DO match .v (not required)
PASS: This is a test.match the m.
PASS: Is this a test?Match the M.
PASS: This is a test!Match the M.
These all fail:
(?<!(i\\.e|e\\.g))[.,!?][a-zA-Z0-9]
(?<!(i\\.e\\.|e\\.g\\.))[.,!?][a-zA-Z0-9]
(?<!(i\\.e|e\\.g))(\\.|\\!|\\?)\\s+“?[a-z]
Current working copy:
(?<!(i|e))[.,!?][a-zA-Z0-9]`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions