Here is my code:
这是我的代码:
var url="https://muijal-ip-dev-ed.my.salesforce.com/apexpages/setup/viewApexPage.apexp?id=066415642TPaE";
In this string i need only
在这个字符串中我只需要
url="https://muijal-ip-dev-ed.my.salesforce.com/"
i need string upto "com/" rest of the string should be removed.
我需要字符串upto“com /”应该删除字符串的其余部分。
5 个解决方案
#1
#2
1
use javascript split
使用javascript拆分
url = url.split(".com");
url = url[0] + ".com";
That should leave you with the wanted string if the Url is well formed.
如果Url格式正确,那么应该给你留下想要的字符串。
#3
1
You can use locate
then substr
like this:
你可以像这样使用locate然后substr:
var url = url.substr(0, url.locate(".com"));
locate
returns you the index of the string searched for and then substr
will cut from the beginning until that index~
locate返回搜索到的字符串的索引,然后substr将从开头剪切到该索引〜
#4
1
Substring function should handle that nicely:
子串函数应该很好地处理:
function clipUrl(str, to, include) {
if (include === void 0) {
include = false;
}
return str.substr(0, str.indexOf(to) + (include ? to.length : 0));
}
console.log(clipUrl("https://muijal-ip-dev-ed.my.salesforce.com/apexpages/setup/viewApexPage.apexp?id=066415642TPaE", ".com", true));
#5
1
If the URL
API (as suggested by another answer) isn't available you can reliably use properties of the HTMLAnchorElement
interface as a workaround if you want to avoid using regular expressions.
如果URL API(由另一个答案建议)不可用,则可以可靠地使用HTMLAnchorElement接口的属性作为解决方法,以避免使用正则表达式。
var a = document.createElement('a');
a.href = 'https://muijal-ip-dev-ed.my.salesforce.com/apexpages/setup/viewApexPage.apexp?id=066415642TPaE';
console.log(a.protocol + '//' + a.hostname);
#1
4
In modern browsers you can use URL()
在现代浏览器中,您可以使用URL()
var url=new URL("https://muijal-ip-dev-ed.my.salesforce.com/apexpages/setup/viewApexPage.apexp?id=066415642TPaE");
console.log(url.origin)
For unsupported browsers use regex
对于不受支持的浏览器,请使用正则表达式
#2
1
use javascript split
使用javascript拆分
url = url.split(".com");
url = url[0] + ".com";
That should leave you with the wanted string if the Url is well formed.
如果Url格式正确,那么应该给你留下想要的字符串。
#3
1
You can use locate
then substr
like this:
你可以像这样使用locate然后substr:
var url = url.substr(0, url.locate(".com"));
locate
returns you the index of the string searched for and then substr
will cut from the beginning until that index~
locate返回搜索到的字符串的索引,然后substr将从开头剪切到该索引〜
#4
1
Substring function should handle that nicely:
子串函数应该很好地处理:
function clipUrl(str, to, include) {
if (include === void 0) {
include = false;
}
return str.substr(0, str.indexOf(to) + (include ? to.length : 0));
}
console.log(clipUrl("https://muijal-ip-dev-ed.my.salesforce.com/apexpages/setup/viewApexPage.apexp?id=066415642TPaE", ".com", true));
#5
1
If the URL
API (as suggested by another answer) isn't available you can reliably use properties of the HTMLAnchorElement
interface as a workaround if you want to avoid using regular expressions.
如果URL API(由另一个答案建议)不可用,则可以可靠地使用HTMLAnchorElement接口的属性作为解决方法,以避免使用正则表达式。
var a = document.createElement('a');
a.href = 'https://muijal-ip-dev-ed.my.salesforce.com/apexpages/setup/viewApexPage.apexp?id=066415642TPaE';
console.log(a.protocol + '//' + a.hostname);