SpaceX
SpaceX

Reputation: 2890

Extract numbers and words from address string using jquery

I have an address string which varies from address to address.

var address_string = '6-3-54/A/889, Road no 45, Toli Chowki, Bangalore, Karnataka 700065, India'

How can I separate the following fields from the above address_string using jQuery

6-3-54/A/889
Road no 45
Toli Chowki
Bangalore
Karnataka
700065
India

Upvotes: 2

Views: 713

Answers (2)

Butani Vijay
Butani Vijay

Reputation: 4239

You can use split function as below.

var arr = address_string.split(',');

Access and print as below:

      for(var i=0;i<arr.length;i++)
      {
          document.write(arr[i]);
      }

Please refer this link ( JS Fiddle) will help to solve you problem

You can use below code to get numbers(array) from String :

String.prototype.getNums= function(){
    var rx=/[+-]?((\.\d+)|(\d+(\.\d+)?)([eE][+-]?\d+)?)/g,
    mapN= this.match(rx) || [];
    return mapN.map(Number);
};

var s= 'I want to extract 123 and 456 from this string ';
alert(s.getNums());

You can check Length as below :

alert(s.getNums()[0].toString().length);

// and so on

Upvotes: 1

mingaleg
mingaleg

Reputation: 2087

Why jQuery?

what_you_need = address_string.split(', ')

Upvotes: 3

Related Questions