You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何用JavaScript正则检测Instagram抽奖评论中至少3个@符号?

Regex Solution for Instagram Giveaway Comment Validation

Hey there! Let's tackle this regex problem for your Instagram giveaway picker. I'll walk you through two solutions: a basic one that checks for at least 3 @ symbols, and a more refined version that ensures those @s are attached to valid Instagram usernames.

Basic Solution: Check for At Least 3 @ Symbols

If you just need to verify that a comment contains at least 3 @ characters (regardless of what comes after them), this simple regex will do the trick:

/@.*@.*@/

Or a more efficient non-capturing group version (great for longer comments):

/(?:@.*?){3}/

How it works:

  • @ matches the @ symbol literally
  • .*? matches any character (except newlines) in a non-greedy way (stops at the next @ instead of gobbling the whole string)
  • (?:...) is a non-capturing group, and {3} requires this group to match 3 times total

Refined Solution: Match Valid Instagram Usernames

Instagram has specific rules for usernames—if you want to avoid false positives (like @@@ or @ @ @), use this regex to ensure each @ tags a valid username:

  • Usernames are 1-30 characters long
  • Only contain letters, numbers, underscores (_), and periods (.)
  • Can't start/end with a period or underscore
  • No consecutive periods/underscores

Here's the regex:

/(?:@[A-Za-z0-9](?:[A-Za-z0-9._]{0,28}[A-Za-z0-9])?){3}/

Breakdown of the username match:

  • @ starts the tag
  • [A-Za-z0-9] ensures the username starts with a letter/number (no leading periods/underscores)
  • (?:[A-Za-z0-9._]{0,28}[A-Za-z0-9])? handles the middle and end:
    • [A-Za-z0-9._]{0,28} allows up to 28 valid characters (keeping total length ≤30)
    • [A-Za-z0-9] ensures the username ends with a letter/number (no trailing symbols)
  • {3} requires at least 3 of these valid tagged usernames

Example Code (Python)

Since you mentioned loading comments into a comments variable, here's how to implement this in Python:

import re

# Your comments list (replace with your actual data)
comments = [
    "@pers1 @pers2 @pers3",          # Valid
    "@user1 @user2",                 # Invalid (only 2 tags)
    "@jane_doe @john.smith @bob123", # Valid (valid usernames)
    "@invalid..name @user2 @user3",  # Invalid (consecutive periods in username)
    "@ @ @",                         # Invalid (no usernames after @)
]

# Compile regex patterns
basic_regex = re.compile(r'(?:@.*?){3}')
refined_regex = re.compile(r'(?:@[A-Za-z0-9](?:[A-Za-z0-9._]{0,28}[A-Za-z0-9])?){3}', re.IGNORECASE)

# Validate each comment
for idx, comment in enumerate(comments, 1):
    print(f"Comment {idx}: {comment}")
    # Basic check
    if basic_regex.search(comment):
        print("  Basic check: PASS (has ≥3 @ symbols)")
    else:
        print("  Basic check: FAIL (fewer than 3 @ symbols)")
    
    # Refined check
    if refined_regex.search(comment):
        print("  Refined check: PASS (has ≥3 valid tagged usernames)")
    else:
        print("  Refined check: FAIL (missing valid tagged usernames)")
    print("---")

Quick Notes

  • Add the re.IGNORECASE flag if you want to match usernames regardless of capitalization (Instagram treats usernames as case-insensitive)
  • The refined regex is stricter and better for a fair giveaway—it filters out comments where @ symbols aren't attached to real-looking usernames

内容的提问来源于stack exchange,提问作者Kinay

火山引擎 最新活动