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 badges
Add a comment  | 

5 Answers 5

Reset to default 21

A 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