替换 URL 无论是 http 还是 https

Replace URL whether http or https

我有一个表单,它接受用户输入的 url 并将旧的 url 替换为新的。用户将输入的 url 格式如下: https://oldproxy.server.url.edu/login?url=https://destinationurl.com. OR http://oldproxy.server.url.edu/login?url=https://destinationurl.com

当用户以 http 开头时,我的脚本成功找到所有实例,但如果它们以 https 开头,则失败。如何包含检查 http 或 https?

<!DOCTYPE html>

<head></head>

<body>
  <script language="javascript">
    <!--//--><![CDATA[// ><!--
    function makeLink() {
      var oin = document.frm.intext;
      var oout = document.frm.outtext;
      var intxt = oin.value;
      if (intxt.length == 0) {
        oin.focus();
        alert("No URL entered!");
      } else {
        //no
        var prep = "https://newproxy.server.url.edu";
        //no
        var rc = intxt.indexOf('.newproxy.server.url.edu/')
        var rd = intxt.indexOf('.newproxy.server.url.edu')
        var wellFormedHttp = intxt.indexOf('http://')
        var wellFormedHttps = intxt.indexOf('https://')
        if (wellFormedHttp == '0' || wellFormedHttps == '0') {
          //alert("Matched http://"+wellFormed); 
          //} 
          if (rc == -1) {
            if (rd == -1) {
              intxt = intxt.replace(/http:\/\/oldproxy.server.url.edu/g, "")
              oout.value = prep + intxt;
              oout.focus();
              oout.select();
            } else {
              alert("dont need to replace");
              intxt = intxt.replace(/.newproxy.server.url.edu/g, "")
              oout.value = prep + intxt;
              oin.focus();
              oin.select();
            }
          } else {
            alert("duplicate");
            oout.value = "";
            oin.focus();
            oin.select();
          }
        } else {
          alert("The URL source URL doesn't start with http:// or https:// or contains multiple entries, please enter a valid URL like https://someaddress.com");
          oout.value = "";
          oin.focus();
          oin.select();
        }
      }

    }

    //--><!]]>
  </script>
  <form name="frm" id="frm">
    <h3>1. Copy and paste your source URL here:</h3>
    <p><textarea aria-label="Source URL" cols="60" name="intext" rows="5"></textarea><br />
    </p>
    <h3>2. Click this:</h3>
    <p><input onclick="makeLink();" type="button" class="btn" value="CONVERT LINK" /><br />
    </p>
    <h3>3. Copy, use, and share the resulting link</h3>
    <p><textarea aria-label="Resulting Link" cols="60" name="outtext" rows="5" id="myInput"></textarea></p>
  </form>
</body>

</html>

您在找这样的东西吗?

下面的示例将整个 url 与正则表达式匹配,并将 url 的捕获组替换为新的 url,从 https://[=12= 开始]

<!DOCTYPE html>

<head></head>

<body>
  <script language="javascript">
    <!--//--><![CDATA[// ><!--
    function makeLink() {
      var oin = document.frm.intext;
      var oout = document.frm.outtext;
      var intxt = oin.value;
      
      if (intxt.length == 0) {
        oin.focus();
        alert("No URL entered!");
      } else {
        var new_url = "newproxyserver.url.edu";
        document.frm.outtext.value = intxt.replaceAll(/(https?:\/\/)(.+?)(\/.+)/gi, `https://${new_url}`);
      }

    }

    //--><!]]>
  </script>
  <form name="frm" id="frm">
    <h3>1. Copy and paste your source URL here:</h3>
    <p><textarea aria-label="Source URL" cols="60" name="intext" rows="5"></textarea><br />
    </p>
    <h3>2. Click this:</h3>
    <p><input onclick="makeLink();" type="button" class="btn" value="CONVERT LINK" /><br />
    </p>
    <h3>3. Copy, use, and share the resulting link</h3>
    <p><textarea aria-label="Resulting Link" cols="60" name="outtext" rows="5" id="myInput"></textarea></p>
  </form>
</body>

</html>