Jak pobrać fragment przed podanym znakiem jQuery lub JavaScript
Próbuję wyodrębnić wszystko przed przecinkiem','. Jak to zrobić w JavaScript lub jQuery? Próbowałem tego i nie działa..
1345 albany street, Bellevue WA 42344
Chcę tylko zdobyć adres.
var streetaddress= substr(addy, 0, index(addy, '.'));
10 answers
var streetaddress= addy.substr(0, addy.indexOf(','));
Chociaż nie jest to najlepsze miejsce na ostateczne informacje o tym, co robi każda metoda (sieć programistów mozilla jest do tego lepsza) w3schools.com jest dobry do wprowadzenia Cię do składni.
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-07-07 00:03:10
var streetaddress = addy.split(',')[0];
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-10-30 13:57:22
Spróbuj tego:
streetaddress.substring(0, streetaddress.indexOf(','));
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-02-03 17:54:25
//split string into an array and grab the first item
var streetaddress = addy.split(',')[0];
Również, polecam nazwanie zmiennych z camel-case (streetAddress) dla lepszej czytelności.
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
2016-03-15 16:41:52
Jeśli podoba Ci się krótki po prostu użyj RegExp :
var streetAddress = /[^,]*/.exec(addy)[0];
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
2014-03-13 17:28:49
Prawie to samo, co odpowiedź Davida G, ale bez funkcji anonimowej, jeśli nie masz ochoty na włączenie jednej.
s = s.substr(0, s.indexOf(',') === -1 ? s.length : s.indexOf(','));
W tym przypadku używamy faktu, że drugi argument substr
jest długością i wiemy, że nasz podłańcuch zaczyna się od 0.
Najlepszą odpowiedzią jest nie ogólne rozwiązanie ze względu na niepożądane zachowanie, jeśli łańcuch znaków nie zawiera znaku, którego szukasz.
Jeśli chcesz poprawnego zachowania w przypadku ogólnym, użyj tej metody lub metody Davida G, a nie najwyższej odpowiedzi
Metody Regex i split również będą działać, ale mogą być nieco wolniejsze / przesadne dla tego konkretnego problemu.
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
2016-03-10 15:58:34
var streetaddress = addy.substr(0, addy.indexOf('.'));
(powinieneś przeczytać javascript tutorial , esp. część o funkcjach łańcuchowych )
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-02-03 17:52:19
Jeśli chcesz zwrócić oryginalny ciąg znaków nietknięty, jeśli nie zawiera znaku wyszukiwania, możesz użyć funkcji anonimowej (zamknięcia):
var streetaddress=(function(s){var i=s.indexOf(',');
return i==-1 ? s : s.substr(0,i);})(addy);
To może być bardziej ogólne:
var streetaddress=(function(s,c){var i=s.indexOf(c);
return i==-1 ? s : s.substr(0,i);})(addy,',');
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-08-21 16:23:16
var newString = string.substr(0,string.indexOf(','));
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-10-30 14:42:38
Możesz również użyć shift()
.
var streetaddress = addy.split( ',' ).shift();
Według MDN Web Docs:
The
shift()
metoda usuwa pierwszy element z tablicy i zwraca ten usunięty element. Metoda ta zmienia długość tablicy.
Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
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-04-24 20:10:59