2

I have a HTML source code having mobile numbers in it. I want to extract just the phone numbers from that source code, each phone number has starting and ending flag. Lets say sample HTML code is, every mobile number starts from 'phone=' and ends with % as shown in below,

<code>
b2e1d163b0b<div class='container'></div>4dc6ebfa<h1>5&amp;t=s&amp;phone=95355036019918%40c.us&amp;i=1522996189s&amp;phone=95355025619123%40c.us&amp;i=1522996189""
</code>

How can I extract all phone numbers using javascript or jquery?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Tasaddaq Rana
  • 127
  • 1
  • 12
  • Please try substr() in Javascript or try https://stackoverflow.com/questions/41515234/extract-a-specific-word-from-string-in-javascript. – Kannan K May 22 '18 at 09:49

4 Answers4

3

You can use RegExp:

var str = "b2e1d163b0b4dc6ebfa5&t=s&phone=95355036019918%40c.us&i=1522996189s&phone=95355025619123%40c.us&i=1522996189";
var reg = /phone=(.*?)\%/g; // Anything between phone= and %
while ((matchArray = reg.exec(str)) !== null) { // Iterate over matchs
  console.log(`Found ${matchArray[1]}.`);
}
Nicolas Cami
  • 458
  • 1
  • 4
  • 13
1

This can be done using indexOf and substr functions

var test="b2e1d163b0b4dc6ebfa5&t=s&phone=95355036019918%40c.us&i=1522996189s&phone=95355025619123%40c.us&i=1522996189"
var start_point = test.indexOf("phone=)+6; 
//indexOf will return the location of "phone=", hence adding 6 to make start_point indicate the starting location of phone number
var phone_number = test.substr(start_location,10);
Irfan Harun
  • 979
  • 2
  • 16
  • 37
0

You can create a custom logic that make use of split() on &phone= and then get the substr() for each item of the splitted array by checking if % exist or not.

var str = "b2e1d163b0b4dc6ebfa5&t=s&phone=95355036019918%40c.us&i=1522996189s&phone=95355025619123%40c.us&i=1522996189";
var strArray = str.split('&phone=');
var phoneNumber = [];
strArray.forEach((item)=>{
  var indexOfPercent = item.indexOf('%');
  if(indexOfPercent !== -1){
    phoneNumber.push(item.substr(0, indexOfPercent));
  }
});
console.log(phoneNumber);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

You can split the items using:

var rawPhoneNumbers = myText.split("phone=");
var phoneNumbers = [];
if (rawPhoneNumbers.length > 1) {
    for (var index = 0; index < rawPhoneNumbers.length; index++) {
        if (rawPhoneNumbers[index].indexOf("%") > -1) {
            phoneNumbers.push(rawPhoneNumbers[index].substring(0, rawPhoneNumbers[index].indexOf("%")));
        }
    }
}
console.log(rawPhoneNumbers);
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175