1

I need to check if a string represents a valid namespace format. A namespace is comprised of ids separated with dots. Each id starts with an alphabetic character and continues with an alphanumeric character.

Valid namespaces:

"com.company.package"
"com.company"
"com"

Invalid namespaces:

"1com.company.package"
"com.1company"
"com.com%any"
".com.company"
"com.company."
"com "
" com"
""
"."
"com..company"

Currently I use this simple regexp but it really don't check all of those invalid namespaces:

if( /^[\w\.]$/.test( namespaceStr ) ) {
  //valid namespace
} else {
  //invalid namespace
}

Any better suggestion for a small and efficient way to check if a string represents a valid namespace?

Here is a little jsfiddle that you can use for testing this regular expression: http://jsfiddle.net/bA85y/

5 Answers 5

5

Edit: This one should work for every case:

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

If you don't care about capturing groups even shorter:

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

A little explanation:

^ // Start
( // Open group
[a-z]\d* // Must start by letter and may be followed by a number (greedy)
(\.[a-z])? // It may be followed by a dot only if it's followed by a letter (non-greedy)
)+ // Close group and match at least once so we get rid of empty values
$ // Ends, not allow any other characters

Demo: http://jsfiddle.net/elclanrs/5hnQV/

Sign up to request clarification or add additional context in comments.

8 Comments

Added a better regex, that should do it for most of those cases.
check this string by your pattern: com.company.package.1
there's something wrong yet! Each id starts with an alphabetic character and continues with an alphanumeric character so why this string can not be valid? com.company4 or c0m
Not sure if thats valid but I think it would be as simple as adding \d* /^(?:[a-z]\d*\.?)+[^.\s\d]$/i. Haven't tried.
+1 good one and very nice example! Could you explain that regex a little furhter pls?
|
2

Try this pattern:

/^[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)*$/i

EDIT: this is a reversion of @elclanrs jsfiddle

3 Comments

Almost the same as mine, but I think yours is better as it doesn't capture the group with the "?:". I think you just should add the 'i' flag.
@Pato, you're right, but I just wanted to show how pattern should be, excepting the flags :D
@HashemQolami: In JS you can use \d instead of 0-9 you can save some bytes right there. Just a tip. xD
2

I think you are looking for this:

/^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*$/i

EDIT:

This one is a little better (with ?: and \d inspired by @HashemQolami and @elclanrs):

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

And this one is shorter but does the same job:

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

And this one too, using lookahead to test that it doesn't end with a .:

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

Please note that the selected answer doesn't work with "a.b.c" or in some cases with more than two levels.

UPDATE:

I've made a little (very basic) test:

var valid = [
"com.company.package",
"com.company",
"com.company1",
"com1.company1",
"a.b.c",
"a1.b.c3.d",
"a1.b2.c3.d4"];

var invalid = [
"1com.company.package",
"com.1company",
"com.com%any",
".com.company",
"com.company.",
"com ",
" com",
"",
".",
"com..company"];

function testRegex(regex, list)
{
    var res=[];
    for(var i=0; i<list.length; i++)
    {
        if(regex.test(list[i]))
            res.push(list[i] + " ==> matched");
        else
            res.push(list[i] + " ==> NOT matched");
    }

    return res.join('<br>');
}

var regex = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*$/i;


var html = "<p>VALID</p>";
html += testRegex(regex, valid);
html += "<p>INVALID</p>";
html += testRegex(regex, invalid);

document.write("<div>" + html + "</div>");

Comments

0

Based on @dionyziz answer this work:

/^[a-z]+(\.[a-z]+)*[^.\s]$/

Comments

-1

The following regular expression will do what you need. It checks for an alphabetic string and then allows multiple other alphabetic strings separated by a dot.

/^[a-z]+(\.[a-z]+)*$/

1 Comment

Thank you @AlexStack, I fixed the regular expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.