-5

I want to get the special string in javascript.

Example:

JAMES SEVERA - 1679581110 - Physician/Psychiatry - 100 EAST BROADWAY, COUNCIL BLUFFS, IA 51503

Here are Doctor's name, position, and address.

I just need the address only. (100 EAST BROADWAY, COUNCIL BLUFFS, IA 51503)

I am going to use this address for the Google map.

ExpertWeblancer
  • 1,368
  • 1
  • 13
  • 28
  • Where is this data coming from? – Robert Jul 27 '20 at 16:06
  • 1
    Note that Stack Overflow is not a coding service. You're expected to try to solve the problem first. If you get stuck somewhere then it's a good idea to ask a specific question here based on your own solution attempt; providing a [MCVE](https://meta.stackoverflow.com/questions/366988/what-does-mcve-mean). Refer to [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask) or the [Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). – tdranv Jul 27 '20 at 16:08

3 Answers3

2

You can use split() function for this. Like this:

var str = "JAMES SEVERA - 1679581110 - Physician/Psychiatry - 100 EAST BROADWAY, COUNCIL BLUFFS, IA 51503";
var address = str.split(" - ")[3];
Zhming
  • 333
  • 3
  • 12
1

If this data is always formatted like

NAME - ID - POSITION - ADDRESS

then you can split by the separator and just take the last element of the array:

const str = "JAMES SEVERA - 1679581110 - Physician/Psychiatry - 100 EAST BROADWAY, COUNCIL BLUFFS, IA 51503";

const strArray = str.split(" - ");

const address = strArray[strArray.length - 1];
Goran
  • 3,292
  • 2
  • 29
  • 32
1

If the string will always be consistent in this format, with address as last and the fields separated by an hyphen then this should do it

const { length, [length - 1]: address } = "JAMES SEVERA - 1679581110 - Physician/Psychiatry - 100 EAST BROADWAY, COUNCIL BLUFFS, IA 51503".split('-');

console.log(address); // prints '100 EAST BROADWAY, COUNCIL BLUFFS, IA 51503'
Qausim
  • 129
  • 2
  • 5