381

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, '.')); 
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Anjana Sharma
  • 4,535
  • 5
  • 37
  • 51

12 Answers12

570
const streetAddress = addy.substring(0, addy.indexOf(","));

While it’s not the best place for definitive information on what each method does (MDN Web Docs are better for that) W3Schools.com is good for introducing you to syntax.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
wheresrhys
  • 22,558
  • 19
  • 94
  • 162
  • 40
    This will not work if the string being searched does not contain an instance of the search character. It will return an empty string. – David G Aug 20 '15 at 19:48
  • 13
    `substr` isn't recommended; should use `substring` instead https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr – Ben Creasy Nov 03 '19 at 01:25
247
var streetaddress = addy.split(',')[0];
user3336882
  • 3,083
  • 2
  • 15
  • 13
47

try this:

streetaddress.substring(0, streetaddress.indexOf(','));
Mikey G
  • 3,473
  • 1
  • 22
  • 27
  • 2
    Why no love for my answer? If this answer is correct http://stackoverflow.com/questions/3745515/what-is-the-difference-between-substr-and-substring my answer is as accurate as the accepted answer, and works for starting indices other than 0. – Mikey G Aug 05 '15 at 20:53
  • 12
    As pointed out by David G above, possibly because it doesn't work if there is no comma. The OP may have implied that the string would always have a comma, but in many instances the delimiter is not guaranteed. See https://jsfiddle.net/13pkp1xn/ – Bumptious Q Bangwhistle Jan 23 '17 at 10:51
41
//split string into an array and grab the first item

var streetaddress = addy.split(',')[0];

Also, I'd recommend naming your variables with camel-case(streetAddress) for better readability.

Alex
  • 21,273
  • 10
  • 61
  • 73
Miles Florence
  • 427
  • 4
  • 5
  • This is better than the method using indexof, in the scenario where there is no comma character – eyal_katz Jan 22 '17 at 19:17
  • 5
    You can save some processing and stop splitting at the first "," found by using what @antak posted as a comment in https://stackoverflow.com/a/22386366/601386: `addy.split(',', 1)[0]`. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split. – flu Oct 12 '17 at 08:16
  • Using array deconstruction, you could do: `const [streetAddress,] = addy.split(',');` This is helpful for cases when you want more than one value, such as: `let [callerIdName, callerId] = callerIdString.split('<');` (original callerIdString format is MCMULLIN,PARKER <+1XXXXXXXXXX>) – parker_codes Aug 31 '18 at 22:37
  • 4
    Isn't this answer exactly the same as the answer posted on Oct 30, 2015? – kojow7 Jan 07 '20 at 20:19
22

If you like it short simply use a RegExp:

var streetAddress = /[^,]*/.exec(addy)[0];
flu
  • 14,307
  • 8
  • 74
  • 71
  • 4
    +1, I think this is a reasonable method, but I took a hint from this and went with `addy.split(',', 1)[0]` – antak Aug 08 '14 at 05:39
  • 3
    This method is very nice for example if you want to split on white space: /[^\s]*/.exec(...) – NuSkooler Aug 01 '15 at 18:08
  • When the remove substring does not neccesarily exist: `streetAddress = addy.replace(/,.*$/, '');`, this removes everything after the first match (of `,`) only if it exists – miile7 Nov 30 '21 at 09:41
21

You can also use shift().

var streetaddress = addy.split(',').shift();

According to MDN Web Docs:

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
13

almost the same thing as David G's answer but without the anonymous function, if you don't feel like including one.

s = s.substr(0, s.indexOf(',') === -1 ? s.length : s.indexOf(','));

in this case we make use of the fact that the second argument of substr is a length, and that we know our substring is starting at 0.

the top answer is not a generic solution because of the undesirable behavior if the string doesn't contain the character you are looking for.

if you want correct behavior in a generic case, use this method or David G's method, not the top answer

regex and split methods will also work, but may be somewhat slower / overkill for this specific problem.

m a
  • 139
  • 1
  • 3
8
var newString = string.substr(0,string.indexOf(','));
Maccath
  • 3,936
  • 4
  • 28
  • 42
Gaurav
  • 28,447
  • 8
  • 50
  • 80
7
var streetaddress = addy.substr(0, addy.indexOf('.')); 

(You should read through a javascript tutorial, esp. the part about String functions)

Mira Weller
  • 2,406
  • 22
  • 27
2

If you want to return the original string untouched if it does not contain the search character then you can use an anonymous function (a closure):

var streetaddress=(function(s){var i=s.indexOf(',');
   return i==-1 ? s : s.substr(0,i);})(addy);

This can be made more generic:

var streetaddress=(function(s,c){var i=s.indexOf(c);
   return i==-1 ? s : s.substr(0,i);})(addy,',');
David G
  • 5,408
  • 1
  • 23
  • 19
2

You could use regex as this will give you the string if it matches the requirements. The code would be something like:

const address = "1345 albany street, Bellevue WA 42344";
const regex = /[1-9][0-9]* [a-zA-Z]+ [a-zA-Z]+/;
const matchedResult = address.match(regex);

console.log(matchedResult[0]); // This will give you 1345 albany street.

So to break the code down. [1-9][0-9]* basically means the first number cannot be a zero and has to be a number between 1-9 and the next number can be any number from 0-9 and can occur zero or more times as sometimes the number is just one digit and then it matches a space. [a-zA-Z] basically matches all capital letters to small letters and has to occur one or more times and this is repeated.

-1

If you are worried about catching the case where no comma is present, you could just do this:

let end = addy.indexOf(",") >= 0 ? addy.indexOf(",") : addy.length;
let streetaddress = addy.substr(0, end);

Nobody said it had to go on one line.

monkey
  • 1,213
  • 2
  • 13
  • 35