Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
Regex for Alphanumeric Values and not two consecutive underscores underscore

I have a requirement in javascript to write a regex for the following condition

The Custom Metadata Record MasterLabel field can only contain underscores and alphanumeric characters. It must be unique, begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.

If we leave out the unique part since we have to check it with the system records i need a regex for testing the other conditions mentioned above.

Here is an expression ive built so far but it doesnt seem to work. I am new to the regular expressions so any help would be appreciated.

/^[a-zA-Z]([a-zA-Z0-9][^ ][^__])[^_]*$/
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The string has all the required properties if and only if it matches the following regular expression:

/^[a-z][a-z\d]*(?:_[a-z\d]+)*$/i

Demo

The regular expression can be broken down as follows.

/
^           # match beginning of string
[a-z]       # match a letter
[a-z\d]*    # match one or more alphanumeric characters
(?:         # begin non-capture group
   _        # match an underscore
  [a-z\d]+  # match one or more alphanumeric characters
)*          # end non-capture group and execute it zero or more times
$           # match end of string
/i          # specify matches of letters to be case-indifferent

Notice that the way I prevent the presence of two underscores in a row has the fortuitous side-effect of ensuring that the last character in the string is not an underscore (a freebie!).

about 4 years ago · Juan Pablo Isaza Report

0

You might be looking for

^(?!_|\d)(?!.*__)(?!.*_$)\w+$

See a demo on regex101.com.


In JavaScript:

const regex = /^(?!_)(?!.*__)(?!.*_$)\w+$/gm;
const str = `_
A
abcd
_
123
some_spaces 
not_any_spaces__
1test34
correct_thing`;
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(`${match}`);
    });
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!