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][^ ][^__])[^_]*$/
You might be looking for
^(?!_|\d)(?!.*__)(?!.*_$)\w+$
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}`);
});
}