Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式和javascript将HTTP url重写为HTTPS_Javascript_Regex - Fatal编程技术网

使用正则表达式和javascript将HTTP url重写为HTTPS

使用正则表达式和javascript将HTTP url重写为HTTPS,javascript,regex,Javascript,Regex,我需要用javascript重写url,并将其从http协议切换到https 我可以将https URL与以下内容匹配: if(url.match('^http://')){ 但是如何使用正则表达式和javascript形成https url呢 url = "https://" + ?; 不能简单地替换http字符串吗 if(url.match('^http://')){ url = url.replace("http://","https://") } 直接替换为正则表达式:

我需要用javascript重写url,并将其从http协议切换到https

我可以将https URL与以下内容匹配:

if(url.match('^http://')){
但是如何使用正则表达式和javascript形成https url呢

url =  "https://" + ?;

不能简单地替换http字符串吗

if(url.match('^http://')){
     url = url.replace("http://","https://")
}

直接替换为正则表达式:

url = url.replace(/^http:\/\//i, 'https://');

根据您的情况,您可能更喜欢切片:

processed_url = "http" + initial_url.slice(5);
http到https的示例:

var initial_url;
var processed_url;

initial_url = "http://stackoverflow.com/questions/5491196/rewriting-http-url-to-https-using-regular-expression-and-javascript";

processed_url = "https" + initial_url.slice(6);

console.log(processed_url)
https到http的示例:

var initial_url;
var processed_url;

initial_url = "https://stackoverflow.com/questions/5491196/rewriting-http-url-to-https-using-regular-expression-and-javascript";

processed_url = "http" + initial_url.slice(5);

console.log(processed_url)

如果用户禁用了javascript?我们的网站有一个免责声明,声明在这种情况下它将不起作用。如果禁用了javascript,则会有很多问题需要解决。安全性不应依赖于浏览器的javascript支持。替换的第一个参数可以是regexpwatch:双反斜杠(//)可能会被混淆为注释(例如,通过脚本捆绑程序清理注释)。更安全的做法是:('http://'+'/')有人能用java代码将其变成可用的形式吗?我尝试将其复制到我的jsp中,但失败了。@AnandVaidya尝试以下操作:
url.replaceFirst((?I)^http://“,“https://”)非常感谢!