admin管理员组文章数量:1193389
I am trying to format a url using regex in javascript...
Input: or
I am using this regex to capture /(http\:\/\/|https\:\/\/){0,1}(.*)/
so $1
is saying is it http
or https
and $2
rest of the url...
Now I want to replace http
with 1
and https
with 2
so that the output should like below:
1www.google
and 2www.yahoo
I used the below code but it's not working...
var url = "";
url.replace(/(http\:\/\/|https\:\/\/){0,1}(.*)/, ("$1"=="http://"?"1":"2")+"$2");
// output: 2www.microsoft
url.replace(/(http\:\/\/|https\:\/\/){0,1}(.*)/, ("$1"=="http://")+"$2");
// output: falsewww.microsoft
Anybody know how to do that...?? thanks...
I am trying to format a url using regex in javascript...
Input: http://www.google.com
or https://www.yahoo.com
I am using this regex to capture /(http\:\/\/|https\:\/\/){0,1}(.*)/
so $1
is saying is it http
or https
and $2
rest of the url...
Now I want to replace http
with 1
and https
with 2
so that the output should like below:
1www.google.com
and 2www.yahoo.com
I used the below code but it's not working...
var url = "http://www.microsoft.com";
url.replace(/(http\:\/\/|https\:\/\/){0,1}(.*)/, ("$1"=="http://"?"1":"2")+"$2");
// output: 2www.microsoft.com
url.replace(/(http\:\/\/|https\:\/\/){0,1}(.*)/, ("$1"=="http://")+"$2");
// output: falsewww.microsoft.com
Anybody know how to do that...?? thanks...
Share Improve this question edited Jul 2, 2014 at 18:10 Naz asked Jul 2, 2014 at 16:55 NazNaz 2,7652 gold badges18 silver badges24 bronze badges5 Answers
Reset to default 21A solution using your regex
var url1= 'http://www.google.com';
var url2= 'https://www.yahoo.com';
url1.replace(/(http\:\/\/|https\:\/\/){0,1}(.*)/, function (g1,g2,g3) {
var prefix ='';
if(g2 == 'https://') {
prefix = '2'
} else{
prefix='1'
}
return prefix + g3
})
//url1.replace(...) //"1www.google.com"
//url2.replace(...) //"2www.yahoo.com"
regexp you are using isn't good: it can match http in the middle of the string and it tries to match whole string even though it is not needed
use this instead
url.replace(/^http(s)?:\/\//, function(match, secure) {
return secure ? "2" : "1"
})
url.replace('https','2').replace('http','1')
Not sure why you would want to do this though
url.replace(/(http\:\/\/|https\:\/\/){0,1}(.*)/,function($1, $2, $3) {
if($2 == 'https://') {
return "2" + $3
} else {
return "1" + $3
}
});
Explanation - How replace function works with RegEx
replace(<RegEx>, handler)
Parameters - $1, $2, $3 ... are result of the RegEx. Each captured group in the regular expression pattern can be used within the replacement string using the $N notation, where "N" is the index of the captured group:
url.replace(/(http\:\/\/|https\:\/\/){0,1}(.*)/, ("$1"=="http://"?"1":"2")+"$2");
should do the trick without having to introduce lambda functions and with minimal modification to your existing code. The problem with your regex is that you were capturing the ://
and not comparing with that, and "http://" == "http"
will always evaluate to false.
本文标签: javascriptjs RegEx conditional replace with captured groupStack Overflow
版权声明:本文标题:javascript - js RegEx conditional replace with captured group - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738423407a2085985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论