简体   繁体   English

Javascript RegEx replace有条件地删除字符串的一部分

[英]Javascript RegEx replace to conditionally remove part of a string

I'm trying to come up with a Javascript RegEx command that will convert these inputs into the following outputs: 我正在尝试使用Javascript RegEx命令将这些输入转换为以下输出:

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" 从域中删除子域,除非子域以字符“dev”开头
  • If the subdomain starts with "dev", then output "dev.domain.com" 如果子域以“dev”开头,则输出“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. 这里的javascript有效,但我希望将它组合成一个正则表达式替换命令。 Note that I also want to retain any port specified at the end of the dmain. 请注意,我还想保留在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';
  }
}

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/ 工作示例: http//jsfiddle.net/gz2tX/

Here's the breakdown: It either matches dev something . 这里的故障:它要么匹配dev 东西 . , or it matches anything . ,或者它匹配任何东西 If it matches the pattern containing "dev", $1 will contain "dev" and $2 will contain ".". 如果它匹配包含“dev”的模式, $1将包含“dev”, $2将包含“。”。 If it doesn't match the pattern containing "dev", $1 and $2 will be empty. 如果它与包含“dev”的模式不匹配,则$1$2将为空。 ( $3 will contain "mydomain.com" in either case.) (在任何一种情况下, $3都将包含“mydomain.com”。)

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. 在我看来,花费时间和精力来计算这一行不值得保存一两行代码长度。

You can use a function to modify the values with more control in JS in a replace. 您可以使用函数在替换中使用JS中的更多控件来修改值。

var value = "dev-foo.mydomain.com".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". 如果第一部分包含单词“dev”,则将其替换为“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. 如果这不是要你想要的,请更新您的问题,指定你想要什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM