const regex = /(?i)\bS(?:ocial|oc\.?|\.)? ?(?!(?:S(?:ecurity|ec\.?|\.)?|Insurance) ?(?:Admin|Insurance|Office|Sec|Tax))(?:(?:S(?:ecurity|ec\.?|\.)?)|(?:Insurance)) ?(?:N(?:\.|o\.?|umbers?|um\.?)?|I\.?D)?(?:\d|#|\.|\b)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?i)\\bS(?:ocial|oc\\.?|\\.)? ?(?!(?:S(?:ecurity|ec\\.?|\\.)?|Insurance) ?(?:Admin|Insurance|Office|Sec|Tax))(?:(?:S(?:ecurity|ec\\.?|\\.)?)|(?:Insurance)) ?(?:N(?:\\.|o\\.?|umbers?|um\\.?)?|I\\.?D)?(?:\\d|#|\\.|\\b)', 'gm')
const str = `######################
# Must Match:
######################
Social Security Number
Social Security Num
Social Security No
Social Security #
Social Security#
Social Security
Social Sec Number
Social Sec Num
Social Sec No
Social Sec #
Social Sec#
Social Sec
Social Sec. Number
Social Sec. Num
Social Sec. No
Social Sec. #
Social Sec.#
Social Sec.
Soc. Sec Number
Soc. Sec Num
Soc. Sec No
Soc. Sec #
Soc. Sec#
Soc. Sec
Soc. Sec. Number
Soc. Sec. Num
Soc. Sec. No
Soc. Sec. #
Soc. Sec.#
Soc. Sec.
Social Insurance Number
Social Insurance Num
Social Insurance No
Social Insurance #
Social Insurance#
SS Num
S.S Num
S.S. Num
SSN#
S.S.N#
S.S.N.#
SSN
S.S.N
S.S.N.
SS #
S.S #
S.S. #
SS#
S.S#
S.S.#
SS ID
SS I.D
SS I.D.
S.S. ID
S.S.I.D.
S.S. I.D
S.S. I.D.
SSID
S.S.I.D
S.S.I.D.
SSN123-
SSN 123
SSN# 123
SSN. 1234
SSN.123
# In context, lowercase:
The following is a list of social insurance numbers:
Yo, get d33z l33t ss# and profit!
######################
# Must Not Match:
######################
# More than social:
Social
Social Number
Social Justice
# No disallowed suffix:
Social Security Administration
S.S. Admin
SS Admin
Social Security Office
S Sec Office
Social Security Tax
# Social required:
Security ID
Security Number
# Invalid combination of security/insurance:
Soc Sec Insurance No
Social Insurance Sec No
`;
// 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