admin管理员组

文章数量:1289876

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

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

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"
  • Retain any port specified at the end of the domain

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

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

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

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

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

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

Here's javascript that works, but I was hoping to bine it into a single regex replace mand. 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\.*)/,'$1.$3');
  }
  else {
    return 'mydomain';
  }
}

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

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

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."
  • Retain any port specified at the end of the domain

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

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

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

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

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

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

Here's javascript that works, but I was hoping to bine it into a single regex replace mand. 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\..*)/,'$1.$3');
  }
  else {
    return 'mydomain.';
  }
}
Share Improve this question edited Feb 26, 2013 at 2:28 Tauren asked Feb 26, 2013 at 2:03 TaurenTauren 27.2k44 gold badges133 silver badges169 bronze badges 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? – jahroy 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. or (something).mydomain. and should always return mydomain., unless (something) contains the word 'dev', in which case it should be dev.mydomain.?? Or you just want to remove any occurences of foo? – mathematical.coffee Commented Feb 26, 2013 at 2:07
  • @jahroy I've updated the question with the rules to use. Maybe you could undo the downvote? – Tauren 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? – Tauren Commented Feb 26, 2013 at 2:26
  • sorry @Tauren, I didn't downvote. Thanks for the extra details. – mathematical.coffee Commented Feb 26, 2013 at 2:39
 |  Show 1 more ment

3 Answers 3

Reset to default 7

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

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

Working example: http://jsfiddle/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." in either case.)

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

My remendation 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.

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

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

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.

本文标签: Javascript RegEx replace to conditionally remove part of a stringStack Overflow