acctman
acctman

Reputation: 4349

RegEx character limits

I want to change the RegEx below too a-Z 0-9 and underscore only

var nameRegex = /^[a-zA-Z]+(([\'\,\.\-][a-zA-Z])?[a-zA-Z]*)*$/;

The 2,4 what does that mean? does that mean check the TLD is a min. of 2 to 4 char. max?

var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

How would I add a required 250 char, would {250,250} at the end do it?

// original
      var messageRegex = 
new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim); 

// would this work?
      var messageRegex = 
new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim){250,250}$/; 

Upvotes: 2

Views: 444

Answers (1)

fncomp
fncomp

Reputation: 6178

If I get your question correctly, then these should be what you're looking for:

  1. Matching a set of characters within A-z, 0-9, and _.

    // Would match any input with A-z 0-9 or _ in any position, given at least one char.
    var nameRegex = /^[A-Za-z\d_]+$/;
    
  2. You're right about the {2,4}. it specifies that [\w-] should match two, three, or four characters.

  3. Your message RegExp will force the input to be exactly 250 characters long to get a match. If you want more than 250 and less than 500, then append {251,500} instead.

  4. Final note, RegExp literals look like /someRegex/ig. If you're using a literal then there's no reason to wrap in the constructor (i.e. new RegExp).

  5. Last note, I, personally avoid * in my RegExp's, since this can lead to fun bug if the star runs into the the closing slash and someone attempts to comment out the expression.

    /* Comment out some code
    var myRe = /[a-z]*/i; // The comment stops before the i.
    */ // This line breaks your code.
    

Edit: Updated point three based on OP feedback.

Upvotes: 3

Related Questions