Javascript Regex to detect common PII

I have a text field and need JavaScript to detect PII using regex. I dont have a coding background. Chatgpt gave me this but nothing is happening. P1 variable is where I am feeding the text to be checked.

function detectPII(p1) {

const patterns = [
    /\b\d{3}-\d{2}-\d{4}\b/, // Social Security Number (SSN)
    /\b\d{3} \d{2} \d{4}\b/, // SSN (alternative format)
    /\b\d{10}\b/, // Phone number (basic format)
    /\b\d{3}[-.\s]\d{3}[-.\s]\d{4}\b/, // Phone number (formatted)
    /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/, // Email address
    /\b\d{16}\b/, // Credit card number (basic check for 16 digits)
    /\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/, // Credit card number (formatted)
    /\b\d{5}(-\d{4})?\b/, // ZIP Code (5 or 9 digits)
    /\b\d{9}\b/, // Basic 9-digit numbers (like a passport or tax ID)
];

// Check if any pattern matches
for (let pattern of patterns) {
    if (pattern.test(p1)) {
        console.log("PII identified");
        return;
    }
}

console.log("None");

}

Hola Joe,

Try with this new syntax:

let result= "None";

const patterns = [
    /\b\d{3}-\d{2}-\d{4}\b/, // Social Security Number (SSN)
    /\b\d{3} \d{2} \d{4}\b/, // SSN (alternative format)
    /\b\d{10}\b/, // Phone number (basic format)
    /\b\d{3}[-.\s]\d{3}[-.\s]\d{4}\b/, // Phone number (formatted)
    /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/, // Email address
    /\b\d{16}\b/, // Credit card number (basic check for 16 digits)
    /\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/, // Credit card number (formatted)
    /\b\d{5}(-\d{4})?\b/, // ZIP Code (5 or 9 digits)
    /\b\d{9}\b/, // Basic 9-digit numbers (like a passport or tax ID)
];
 
// Check if any pattern matches
for (let pattern of patterns) {
    if (pattern.test(p1))  
        result ="PII identified";
}
 
return result

Saludos!

Awesome, that works! Any idea outside of just “PII identified, it can provide the type of PII and the actual PII?

Ex: “PII identified - SSN - 001-11-1111”

or if multiple PII identified:

Ex: “PII identified - SSN: 001-11-1111 DOB: 01/01/1900”

Try this:


return detectPII(p1)

function detectPII(p1) {
    const patterns = [
        { type: 'SSN', pattern: /\b\d{3}-\d{2}-\d{4}\b/ },
        { type: 'SSN', pattern: /\b\d{3} \d{2} \d{4}\b/ },
        { type: 'Phone', pattern: /\b\d{10}\b/ },
        { type: 'Phone', pattern: /\b\d{3}[-.\s]\d{3}[-.\s]\d{4}\b/ },
        { type: 'Email', pattern: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/i },
        { type: 'Credit Card', pattern: /\b\d{16}\b/ },
        { type: 'Credit Card', pattern: /\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/ },
        { type: 'ZIP Code', pattern: /\b\d{5}(-\d{4})?\b/ },
        { type: '9-digit ID', pattern: /\b\d{9}\b/ },
    ];

    const matchesFound = [];

    // Check all patterns
    for (const { type, pattern } of patterns) {
        const regex = new RegExp(pattern.source, 'g');
        let match;
        while ((match = regex.exec(p1)) !== null) {
            matchesFound.push({
                type: type,
                value: match[0]
            });
        }
    }

    // Remove duplicate matches
    const uniqueMatches = matchesFound.filter((value, index, self) =>
        index === self.findIndex((t) => (
            t.type === value.type && t.value === value.value
        ))
    );

    if (uniqueMatches.length === 0) return 'None';

    // Format the output
    const resultString = uniqueMatches.map(match => `${match.type}: ${match.value}`).join(' ');
    return `PII identified - ${resultString}`;
}

For some reason I am getting Function Error SyntaxError: Unexpected Token ‘}’

And no result

The error is self-explanatory, you have a dangling curly bracket somewhere.
The code that Thinh supplied runs without error as it is. Did you change it?

I literally just used the copy button and pasted it into a brand new Javascript column. :man_shrugging:t2:

Can you show me?

Needed to create an account with my company as they dont allow personal Google sign ups lol. Just waiting for the new account to be approved.

Approved :wink:

Very hard to tell from your screenshot.
But one thing I did just realise is that Thinh just gave you a function.
That function needs to be called - just copy/pasting into a JavaScript column as it is won’t work.

Try adding the following right at the top:

return detectPII(p1);

This is what I get:

2 Likes

That worked! Awesome ty

1 Like

Thank you, edited my original function to have the return line at the top.

1 Like