const regex = /\b(?!\w\s{1,2}\w+)\w(\w|\s){3}\d{10}/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\b(?!\\w\\s{1,2}\\w+)\\w(\\w|\\s){3}\\d{10}', 'gm')
const str = ` a regular expression using C#/.Net that matches 1-4 alphanumerics followed by spaces, followed by 10 digits.
The catch is the number of spaces plus the number of alphanumerics must equal 4,
and the spaces must follow the alphanumerics, not be interspers
abcd1234567890 y
a 1234567890 y
ab 1234567890 y
1 1234567890 y
1 1234567890 n
1 1234567890 n
11234567890 n
a c 1234567890 n
a d1234567890 n
abcd 1234567890 n
`;
// 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