admin管理员组文章数量:1431955
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:
- www.example --> converts to --->
- --> 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:
- www.example. --> converts to ---> https://www.example.
- 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.
-
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
7 Answers
Reset to default 4There 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
版权声明:本文标题:unable to find string using indexOf in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745574168a2664227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论