# Javascript Regex to detect common PII

**URL:** <https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798>\
**Category:** Ask for Help\
**Created:** [March 17, 2025, 6:42pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798 "2025-03-17T18:42:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Gabriele](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/joe_gabriele/32/57595_2.png) [@Joe\_Gabriele](https://community.glideapps.com/u/Joe_Gabriele)\
**Post date:** [March 17, 2025, 6:42pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/1 "2025-03-17T18:42:46Z")

</div>

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");
> 
> ```
> 
> }

---

<div class="post-metadata">

**Author:** ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)\
**Post date:** [March 17, 2025, 8:00pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/2 "2025-03-17T20:00:40Z")

</div>

Hola Joe,

Try with this new syntax:

```auto
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!

---

<div class="post-metadata">

**Author:** ![Joe\_Gabriele](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/joe_gabriele/32/57595_2.png) [@Joe\_Gabriele](https://community.glideapps.com/u/Joe_Gabriele)\
**Post date:** [March 17, 2025, 8:12pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/3 "2025-03-17T20:12:16Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [March 18, 2025, 12:42am UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/4 "2025-03-18T00:42:15Z")

</div>

Try this:

```auto

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}`;
}

```

 ![CleanShot 2025-03-18 at 07.42.01@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/d/3d10e4816c0126e7db7b2adfbe49cdf2651df670.png)

---

<div class="post-metadata">

**Author:** ![Joe\_Gabriele](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/joe_gabriele/32/57595_2.png) [@Joe\_Gabriele](https://community.glideapps.com/u/Joe_Gabriele)\
**Post date:** [March 18, 2025, 11:56am UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/5 "2025-03-18T11:56:16Z")

</div>

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

And no result

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 18, 2025, 12:40pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/6 "2025-03-18T12:40:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Joe\_Gabriele](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/joe_gabriele/32/57595_2.png) [@Joe\_Gabriele](https://community.glideapps.com/u/Joe_Gabriele)\
**Post date:** [March 18, 2025, 12:41pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/7 "2025-03-18T12:41:21Z")

</div>

I literally just used the copy button and pasted it into a brand new Javascript column. 🤷🏻‍♂️

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 18, 2025, 1:11pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/8 "2025-03-18T13:11:46Z")

</div>

Can you show me?

---

<div class="post-metadata">

**Author:** ![Joe\_Gabriele](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/joe_gabriele/32/57595_2.png) [@Joe\_Gabriele](https://community.glideapps.com/u/Joe_Gabriele)\
**Post date:** [March 18, 2025, 1:26pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/9 "2025-03-18T13:26:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jk18gabr](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jk18gabr/32/83327_2.png) [@jk18gabr](https://community.glideapps.com/u/jk18gabr)\
**Post date:** [March 18, 2025, 1:37pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/10 "2025-03-18T13:37:40Z")

</div>

![1c9b522f-c2dc-47cd-a938-169cedc4ca2c](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/e/c/ec8c7f8f0bf44a0786f59afc7c65487d2f3b6f51.jpeg)

 ![678d1f9d-6b88-4a5f-b042-1b701b22a76b](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/f/4/f44fac7e25ddb185e660215e8474fe405ce0d9f9.jpeg)

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 18, 2025, 1:37pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/11 "2025-03-18T13:37:58Z")

</div>

Approved 😉

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [March 18, 2025, 1:42pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/12 "2025-03-18T13:42:50Z")

</div>

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:

```auto
return detectPII(p1);

```

This is what I get:

 ![CleanShot 2025-03-18 at 21.42.10@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/1/3/1366f9713da08e38fea643a6dc57a706b5e58fee.jpeg)

---

<div class="post-metadata">

**Author:** ![jk18gabr](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jk18gabr/32/83327_2.png) [@jk18gabr](https://community.glideapps.com/u/jk18gabr)\
**Post date:** [March 18, 2025, 1:43pm UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/13 "2025-03-18T13:43:49Z")

</div>

> [@Darren\_Murphy](#):
>
> `return detectPII(p1);`

That worked! Awesome ty

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [March 19, 2025, 12:15am UTC](https://community.glideapps.com/t/javascript-regex-to-detect-common-pii/80798/14 "2025-03-19T00:15:27Z")

</div>

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