I am trying to extract everything before the ',' comma. How do I do this in JavaScript or jQuery? I tried this and not working..
1345 albany street, Bellevue WA 42344
I just want to grab the street address.
var streetaddress= substr(addy, 0, index(addy, '.'));
Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.
Alternatively, you can use the str. split() method. To get the substring after a specific character: Use the split() method to split the string on the character.
The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.
The difference between substring() and substr()The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .
var streetaddress= addy.substr(0, addy.indexOf(','));
While it's not the best place for definitive information on what each method does (mozilla developer network is better for that) w3schools.com is good for introducing you to syntax.
var streetaddress = addy.split(',')[0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With