2017-06-01 10 views
1

は私のコードです:この文字列で特定の単語の後に文字列を分割したいですか?ここ

var url="https://muijal-ip-dev-ed.my.salesforce.com/apexpages/setup/viewApexPage.apexp?id=066415642TPaE"; 

私は、文字列の「COM /」静止点で最大の文字列を削除する必要がある必要があるだけ

url="https://muijal-ip-dev-ed.my.salesforce.com/" 

を必要としています。 URLが十分に形成されている場合、希望の文字列を残す必要があります

url = url.split(".com"); 
url = url[0] + ".com"; 

を分割するJavaScript

答えて

1

使用。

あなたが locate、その後 substrこのように使用することができます
+0

また、javascriptを[ストリング](https://www.w3schools.com/jsref/jsref_substring.asp) –

1

:あなたは文字列のインデックスを検索し、その後substrlocate

var url = url.substr(0, url.locate(".com")); 

リターンが最近のブラウザでは〜そのインデックスまで最初からあなたを

4

をカットしますURL()

var url=new URL("https://muijal-ip-dev-ed.my.salesforce.com/apexpages/setup/viewApexPage.apexp?id=066415642TPaE"); 
 

 
console.log(url.origin)
012を使用することができますサポートされていないブラウザのために

use regex

1

SUBSTRING関数はうまくそれを処理する必要があります

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));

1

another answerにより示唆されるように)URL APIは、あなたが確実にできる利用できない場合回避する場合は、HTMLAnchorElementインターフェイスのプロパティを回避策として使用してくださいg正規表現。

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

ザッツの素敵をチェックすることができます。私はこれまで、URLの前にどのようにしていたのか把握しようとしていました – sabithpocker

関連する問題