Sprawdź, czy ciąg zaczyna się od czegoś? [duplikat]

Możliwy Duplikat:
Javascript StartsWith

Wiem, że mogę zrobić jak^=, aby zobaczyć, czy identyfikator zaczyna się od czegoś, i próbowałem użyć tego do tego, ale to nie działa... Zasadniczo pobieram adres url i chcę ustawić klasę dla elementu dla nazw ścieżek, które zaczynają się w określony sposób...

Więc,

var pathname = window.location.pathname;  //gives me /sub/1/train/yonks/459087

Chcę się upewnić, że dla każdej ścieżki zaczynającej się od /sub/1, Mogę ustawić klasę dla elementu...

if(pathname ^= '/sub/1') {  //this didn't work... 
        ... 
Author: Community, 2009-11-20

6 answers

Użyj stringObject.substrat

if (pathname.substring(0, 6) == "/sub/1") {
    // ...
}
 356
Author: Philip Reynolds,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-20 19:31:36
String.prototype.startsWith = function(needle)
{
    return this.indexOf(needle) === 0;
};
 175
Author: Ricardo Peres,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-08 16:58:00

Możesz użyć string.match () i wyrażenie regularne dla tego też:

if(pathname.match(/^\/sub\/1/)) { // you need to escape the slashes

string.match() zwróci tablicę pasujących podłańcuchów, jeśli zostanie znaleziona, w przeciwnym razie null .

 82
Author: Cros,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-11 22:42:32

Trochę więcej funkcji wielokrotnego użytku:

beginsWith = function(needle, haystack){
    return (haystack.substr(0, needle.length) == needle);
}
 36
Author: RobKohr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-06 23:15:25

Najpierw pozwala rozszerzyć obiekt string. Dzięki Ricardo Peresowi za prototyp, myślę, że użycie zmiennej "string" działa lepiej niż "igła" w kontekście uczynienia go bardziej czytelnym.

String.prototype.beginsWith = function (string) {
    return(this.indexOf(string) === 0);
};

Potem używasz go w ten sposób. Uwaga! Sprawia, że kod jest niezwykle czytelny.

var pathname = window.location.pathname;
if (pathname.beginsWith('/sub/1')) {
    // Do stuff here
}
 22
Author: Tim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-19 06:31:54

Spójrz na JavaScript substring() metoda.

 2
Author: rochal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-03 23:12:45