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

5 Answers 5

Reset to default 4

In 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