替换URL,无论是http还是https



我有一个表单,它接受用户输入的url,并用新的url替换旧的url。用户将输入的url格式如下:https://oldproxy.server.url.edu/login?url=https://destinationurl.com.或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://开始

<!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}$3`);
}
}
//--><!]]>
</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>

最新更新