skip to Main Content

I’m moving up from Photoshop scripts to something else:
Using JavaScript I need to check the validity of website address for three things.

  • A dot (.) before and after “somesite”
  • Nothing between “.somesite” and “.com/”
  • Nothing between “.com” and the “/”

In my example I’m looking at www.somesite.com

Here’s the code so far:

URIArr = [
"https://www.somesite.com/find-work-home",
"www.somesite.com/",
"blahsomesite.com/bananas/stuff",
"something.somesite.com/bananas/cheese",
"blahsomesite.com/bananas/123",
"www.blah.somesite.m.com/bananas/5678",
"blah.somesite.comm/bananas/ook",
]


for (var i = 0; i < URIArr.length; i++)
{
    var temp = URIArr[i];
    var valid = checkURL(".somesite", temp);
    if (!valid)
    {
        alert(temp + " is " + checkURL(".somesite", temp));
    }
}

function removeTrailingSlashes(site)     
{     
    return site.replace(//$/, "");
} 

function checkURL(webstr, str)
{

// A dot (.) before and after "somesite"
// Nothing between ".somesite" and ".com/"
// Nothing between ".com" and the "/"

    var test1 = false;
    var test2 = false;   
    var test3 = false;   
    var c = ".com";
    var haystack = str.toLowerCase();
    var needle = webstr.toLowerCase();
    haystack = removeTrailingSlashes(haystack);

    if (!haystack.charAt(haystack .length) === "/")
    haystack  += "/";

    var n = haystack.indexOf(needle);
    var m = n + (needle.length);

    // first check
    if (str.charAt(n) && str.charAt(m) === ".") test1 = true;

    //second check
    var o = haystack.indexOf(c);
    if (o-m === 0) test2 = true;

    // third check
    var p = o + (c.length);
    var truncStr = haystack.substring(o, haystack.length);
    var q = truncStr.indexOf("/") + o; 
    if (q-p === 0) test3 = true;

    // final triplecheck
    if ((test1 == true) && (test2 == true) && (test3== true))  return true 
     return false
}

The question is this:
– Did I miss any tricks (I noticed that for the third condition I had to add trailing slashes – even though they might not be present)
But more importantly:
– Could this this be reworked with (three) regular expressions?

Is this a job for Reginald X. Pression?

2

Answers


  1. You can use the following single regex for all three tests:

    .somesite(?=.com/)
    

    Js code:

    var regex = /.somesite(?=.com/)/g;
    var valid = regex.test(myString);    //true if found.. else false
    

    See DEMO

    Login or Signup to reply.
  2. Hmm I feel like your three conditions of:

    • A dot (.) before and after “somesite”
    • Nothing between “.somesite” and “.com/”
    • Nothing between “.com” and the “/”

    can be simplify to 1 condition:

    • .somesite.com/

    And if that is so, actually without using regex, you can solve it via:

    var valid = somestring.indexOf(".somesite.com/") > -1;
    

    using regex would be:

    var valid = somestring.match(/.somesite.com//);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search