admin管理员组

文章数量:1022533

I am using the function to set the https to the url passed in input.

setHttps(a) {
    if (a.indexOf('http://') > 0){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

When I pass the following:

  1. www.example --> converts to --->
  2. --> doesn't get converted to --->

Why? It looks like indexOf('http://') is unable to find http:// in the string.

I am using the function to set the https to the url passed in input.

setHttps(a) {
    if (a.indexOf('http://') > 0){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

When I pass the following:

  1. www.example. --> converts to ---> https://www.example.
  2. http://www.example. --> doesn't get converted to ---> https://www.example.

Why? It looks like indexOf('http://') is unable to find http:// in the string.

Share Improve this question edited Dec 28, 2016 at 9:11 kittu asked Dec 28, 2016 at 9:09 kittukittu 7,02821 gold badges103 silver badges199 bronze badges 2
  • 7 a.indexOf('http://') > -1 – Flying Gambit Commented Dec 28, 2016 at 9:10
  • May restrict the replacement to the start of the url, e. g. google./search?q=site:http://test.+test – Jonas Wilms Commented Dec 28, 2016 at 9:33
Add a ment  | 

7 Answers 7

Reset to default 4

There are atleast 2 issues in your code:

a.indexOf('http://') > 0    // should be >= 0
a.replace('http', 'https')  // replace returns a String  

Try the following:

setHttps(a) {
var a = self.previewUrl.value;
if (a.indexOf('http://') >= 0){
        a = a.replace('http', 'https')
        console.log('priting url if...... ', a);
    } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
        a = a.replace (/^/,'https://');
        console.log("printing url else........ ", a);
    } 
return a;

}

You need to update the variable with returned value. Although use http:// instead of http to avoid matching http in https and index starts from 0 so update condition based on that.

if (a.indexOf('http://') > -1){
    a = a.replace('http://', 'https://')
}

UPDATE : In the else if condition a.indexOf('http://') == -1 is not necessary since it already checked in first if.

setHttps(a) {
    if (a.indexOf('http://') > -1){
            a= a.replace('http://', 'https://')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1) {
            a = a.replace (/^/,'https://');
            // or simply concat
            // a = 'https://' + a;
            console.log("printing url else........ ", a);
        } 
    return a;
}

In JavaScript grammar "-1" is usually used as sentinel value, and it es from C.

String.prototype.indexOf method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

So you'll probably want to do something like :

setHttps(a) {
    var a = self.previewUrl.value;
    if (a.indexOf('http://') > -1){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

Read about indexOf

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Update:

if(a.indexOf('http://') > -1){
   a = a.replace("http", "https");
}else if(a.indexOf('https') === -1){
   a = "https://" + a;
}

Check with this code

setHttps(a) {
    if (a.indexOf('http://') > -1){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

Change

if (a.indexOf('http://') > 0)

to

if (a.indexOf('http://') > -1)

"http://www.example.".indexOf('http://') outputs 0 as 'http://' is at that index in the provided URL.

Also, you need to assign the a.replace's output back to a, so the final code would look like this:

function setHttps(a) {
    if (a.indexOf('http://') > -1){
            a = a.replace('http', 'https');
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

You need to replace this

if (a.indexOf('http://') > 0)

to

if (a.indexOf('http://') > -1)

It is because, indexOf() returns the index of the first occurrence of the string. Here in your case, the first occurrence of http:// is 0. But, your condition is true if indexOf() > 0. So you need to change the condition so that it would work for 0 also.

I am using the function to set the https to the url passed in input.

setHttps(a) {
    if (a.indexOf('http://') > 0){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

When I pass the following:

  1. www.example --> converts to --->
  2. --> doesn't get converted to --->

Why? It looks like indexOf('http://') is unable to find http:// in the string.

I am using the function to set the https to the url passed in input.

setHttps(a) {
    if (a.indexOf('http://') > 0){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

When I pass the following:

  1. www.example. --> converts to ---> https://www.example.
  2. http://www.example. --> doesn't get converted to ---> https://www.example.

Why? It looks like indexOf('http://') is unable to find http:// in the string.

Share Improve this question edited Dec 28, 2016 at 9:11 kittu asked Dec 28, 2016 at 9:09 kittukittu 7,02821 gold badges103 silver badges199 bronze badges 2
  • 7 a.indexOf('http://') > -1 – Flying Gambit Commented Dec 28, 2016 at 9:10
  • May restrict the replacement to the start of the url, e. g. google./search?q=site:http://test.+test – Jonas Wilms Commented Dec 28, 2016 at 9:33
Add a ment  | 

7 Answers 7

Reset to default 4

There are atleast 2 issues in your code:

a.indexOf('http://') > 0    // should be >= 0
a.replace('http', 'https')  // replace returns a String  

Try the following:

setHttps(a) {
var a = self.previewUrl.value;
if (a.indexOf('http://') >= 0){
        a = a.replace('http', 'https')
        console.log('priting url if...... ', a);
    } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
        a = a.replace (/^/,'https://');
        console.log("printing url else........ ", a);
    } 
return a;

}

You need to update the variable with returned value. Although use http:// instead of http to avoid matching http in https and index starts from 0 so update condition based on that.

if (a.indexOf('http://') > -1){
    a = a.replace('http://', 'https://')
}

UPDATE : In the else if condition a.indexOf('http://') == -1 is not necessary since it already checked in first if.

setHttps(a) {
    if (a.indexOf('http://') > -1){
            a= a.replace('http://', 'https://')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1) {
            a = a.replace (/^/,'https://');
            // or simply concat
            // a = 'https://' + a;
            console.log("printing url else........ ", a);
        } 
    return a;
}

In JavaScript grammar "-1" is usually used as sentinel value, and it es from C.

String.prototype.indexOf method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

So you'll probably want to do something like :

setHttps(a) {
    var a = self.previewUrl.value;
    if (a.indexOf('http://') > -1){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

Read about indexOf

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Update:

if(a.indexOf('http://') > -1){
   a = a.replace("http", "https");
}else if(a.indexOf('https') === -1){
   a = "https://" + a;
}

Check with this code

setHttps(a) {
    if (a.indexOf('http://') > -1){
            a.replace('http', 'https')
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

Change

if (a.indexOf('http://') > 0)

to

if (a.indexOf('http://') > -1)

"http://www.example.".indexOf('http://') outputs 0 as 'http://' is at that index in the provided URL.

Also, you need to assign the a.replace's output back to a, so the final code would look like this:

function setHttps(a) {
    if (a.indexOf('http://') > -1){
            a = a.replace('http', 'https');
            console.log('priting url if...... ', a);
        } else if(a.indexOf('https://') == -1 && a.indexOf('http://') == -1) {
            a = a.replace (/^/,'https://');
            console.log("printing url else........ ", a);
        } 
    return a;
}

You need to replace this

if (a.indexOf('http://') > 0)

to

if (a.indexOf('http://') > -1)

It is because, indexOf() returns the index of the first occurrence of the string. Here in your case, the first occurrence of http:// is 0. But, your condition is true if indexOf() > 0. So you need to change the condition so that it would work for 0 also.

本文标签: unable to find string using indexOf in javascriptStack Overflow