2

I'm trying to come up with a Javascript RegEx command that will convert these inputs into the following outputs:

INPUT                      DESIRED OUTPUT
mydomain.com          -->  mydomain.com
foo.mydomain.com      -->  mydomain.com
dev.mydomain.com      -->  dev.mydomain.com
dev-foo.mydomain.com  -->  dev.mydomain.com

Here's the rules:

  • Remove the subdomain from the domain unless the subdomain starts with the characters "dev"
  • If the subdomain starts with "dev", then output "dev.domain.com"
  • Retain any port specified at the end of the domain

My regex skills are failing me. This is what I have so far:

'mydomain.com'.replace(/^(dev)?(-.*)?(mydomain.com)/,'$1.$3'); 
// .mydomain.com

'foo.mydomain.com'.replace(/^(dev)?(-.*)?(mydomain.com)/,'$1.$3'); 
// foo.mydomain.com

'dev-foo.mydomain.com'.replace(/^(dev)?(-.*)?(mydomain.com)/,'$1.$3'); 
// dev.mydomain.com

'dev.mydomain.com'.replace(/^(dev)?(-.*)?(mydomain.com)/,'$1.$3'); 
// dev.mydomain.com

The first two fail and the last two work. Any suggestions?

Here's javascript that works, but I was hoping to combine it into a single regex replace command. Note that I also want to retain any port specified at the end of the dmain.

var getHost = function () {
  var host = window.location.host;
  if (host.substring(0,3) === 'dev') {
    return host.replace(/^(dev)?(-.*)?(mydomain\.com.*)/,'$1.$3');
  }
  else {
    return 'mydomain.com';
  }
}
6
  • 1
    Could you please explain the general rule that should apply here? Based on your example input/output, one might assume that you're looking for a way to remove hyphens, periods, and the word foo when they appear before the first period. Is that what you want? Commented Feb 26, 2013 at 2:07
  • 1
    Can you explain a little more what you want your regex to work? so it takes in a string of the form mydomain.com or (something).mydomain.com and should always return mydomain.com, unless (something) contains the word 'dev', in which case it should be dev.mydomain.com?? Or you just want to remove any occurences of foo? Commented Feb 26, 2013 at 2:07
  • @jahroy I've updated the question with the rules to use. Maybe you could undo the downvote? Commented Feb 26, 2013 at 2:25
  • @mathematical.coffee Sorry I left out some critical details. I've updated the question with more info. Maybe you could undo the downvote? Commented Feb 26, 2013 at 2:26
  • sorry @Tauren, I didn't downvote. Thanks for the extra details. Commented Feb 26, 2013 at 2:39

3 Answers 3

7

Just for the sake of showing that it's possible to do in a single regular expression:

'string'.replace(/(?:(^dev).*(\.)|^.*)(mydomain\.com)/, '$1$2$3');

Working example: http://jsfiddle.net/gz2tX/

Here's the breakdown: It either matches devsomething., or it matches anything. If it matches the pattern containing "dev", $1 will contain "dev" and $2 will contain ".". If it doesn't match the pattern containing "dev", $1 and $2 will be empty. ($3 will contain "mydomain.com" in either case.)

So it's possible, but it's convoluted.

My recommendation is to do something more readable and maintainable than this. The time and pain spent figuring out this line is not worth saving a line or two of code length in my opinion.

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

4 Comments

+1 for "do something more readable and maintanable". I don't see any benefit to using a regex here.
Nice job @wiseguy! I completely agree with your sentiment about readability and maintainability. But my question was just a simple example of what I'm really trying to do. In my situation, it makes more sense to encapsulate it into a single regex.
@jahroy It feels kind of goofy saying, "Here's your answer, now please don't use it." lol
@Wiseguy Yes, your solution will work. Very much appreciated. I'd just like to make the domain generic instead of "mydomain" so that it can be reused with other domains. But that wasn't part of the question. It should use the domain from the original string.
2

You can use a function to modify the values with more control in JS in a replace.

var value = "dev-foo.mydomain.com".replace(/^(dev)?[^\.]+\./, function (match, p1) {
    return p1 ? p1 + '.' : '';
});

1 Comment

Ah, I think you're onto the solution here. Your code doesn't work for mydomain.com or dev.mydomain.com, but I think this is what I was looking for.
1

It's hard to tell what you're asking for...

But... based on your sample input/output you could do the following:

  1. split the input on the first period.
  2. if the first portion contains the word "dev" replace it with "dev".
  3. if the first portion does NOT contain the word dev, remove it.

No need for regex if that's actually what you want:

function trimInput(s) {
    var n = s.indexOf('.');
    if (n === -1) {
        return s;
    }
    var head = s.substring(0, n);
    if (head.indexOf("dev") !== -1) {
        return "dev." + s.substring(n);
    }
    else {
        return s.substring(n);
    }
}

If that's NOT want you want, please update your question to specify what you do want.

2 Comments

Yes, your description explains what I want. But my objective obviously wasn't clear in that I'd like to reduce this into a single regex if possible.
+1 Thanks for a readable answer. In most situations this would be preferred.

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.