admin管理员组文章数量:1277324
Here is my code:
var url=".apexp?id=066415642TPaE";
In this string i need only
url="/"
i need string upto "/" rest of the string should be removed.
Here is my code:
var url="https://muijal-ip-dev-ed.my.salesforce./apexpages/setup/viewApexPage.apexp?id=066415642TPaE";
In this string i need only
url="https://muijal-ip-dev-ed.my.salesforce./"
i need string upto "/" rest of the string should be removed.
Share Improve this question asked Jun 1, 2017 at 13:19 Salesforce CoimbatoreSalesforce Coimbatore 411 silver badge3 bronze badges5 Answers
Reset to default 4In modern browsers you can use URL()
var url=new URL("https://muijal-ip-dev-ed.my.salesforce./apexpages/setup/viewApexPage.apexp?id=066415642TPaE");
console.log(url.origin)
For unsupported browsers use regex
use javascript split
url = url.split(".");
url = url[0] + ".";
That should leave you with the wanted string if the Url is well formed.
You can use locate
then substr
like this:
var url = url.substr(0, url.locate("."));
locate
returns you the index of the string searched for and then substr
will cut from the beginning until that index~
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./apexpages/setup/viewApexPage.apexp?id=066415642TPaE", ".", true));
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.
var a = document.createElement('a');
a.href = 'https://muijal-ip-dev-ed.my.salesforce./apexpages/setup/viewApexPage.apexp?id=066415642TPaE';
console.log(a.protocol + '//' + a.hostname);
本文标签: javascriptwant to split a string after a certain wordStack Overflow
版权声明:本文标题:javascript - want to split a string after a certain word? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741281337a2370024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论