AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



javascript-regex: Extract all emails phone numbers date time from a large text

I try to exract all emails phone nmbers, dates, time and information after the time(one or two words after time). Help me please!!!

<html>

<head>
<title>javascript regex</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
<meta charset="utf-8">
</head>

<body>
<p id="number"> ygjyjgyjgj myemail@gmail.com rjegergje. erertergergrege ergerge wwerttyrtr@gmail.com fthftet ft +7889983453 tyjt tydyj. yutututrt7jyur 12.10.2017. rtyrtyrurtuty yujyky gy ugy 17:00 fhfhhfjf drthdh +3809611350dthrdhrtdh dthdrrhttrdhr.</p>
<button type="button" id="button" name="button">Press</button>
<script>
button.onclick = function() {
var str, regExp, dataArr, email, phone, date, time, remains;
str = ´ygjyjgyjgj myemail@gmail.com rjegergje. erertergergrege ergerge wwerttyrtr@gmail.com fthftet ft +1889983453 tyjt tydyj. yutututrt7jyur 12.10.2017. rtyrtyrurtuty yujyky gy ugy 17:00 fhfhhfjf drthdh +1889983402 dthrdhrtdh dthdrrhttrdhr.´;
var number = document.getElementById(´number´);
var textContent = number.innerHTML;
regExp = /([\w\.]+@[\w]+\.[\w\.]+)[.]*[.\s\w]*(\+[\d]+)[.\s\w]*(\d{2}\.\d{2}\.\d{4})[.\s\w]*(\d{2}:\d{2})([.\s\w]*)/;
dataArr = regExp.exec(str);
email = dataArr[1];
phone = dataArr[2];
date = dataArr[3];
time = dataArr[4];
remains = dataArr[5];
document.writeln(email, ´<br>´, phone, ´<br>´, date, ´<br>´, time, ´<br>´, remains);

alert(number);
alert(email);
alert(date);
alert(time);

};
</script>
</body>

</html>

javascript x 28
regex x 7
Posted On : 2017-11-16 23:26:10.0
profile Aryan Gupta - anyforum.in Aryan Gupta
600
up-rate
2
down-rate

Answers


If you are looking for exact combination, As per your input, It should return only one object. Just put "gi" at the end of your regular expression. Other way if you just want to extract all the emails, phones, dates, times just split your regular expression and extract it one by one as shown below.

<html>

<head>
<title>javascript regex</title>
<meta charset="utf-8">
</head>

<body>
<p id="number"> ygjyjgyjgj myemail@gmail.com rjegergje. erertergergrege ergerge wwerttyrtr@gmail.com fthftet ft +7889983453 tyjt tydyj. yutututrt7jyur 12.10.2017. rtyrtyrurtuty yujyky gy ugy 17:00 fhfhhfjf drthdh +3809611350dthrdhrtdh dthdrrhttrdhr.</p>
<button type="button" id="button" name="button">Press</button>
<script>
button.onclick = function() {
var str, regExp, dataArr, email, phone, date, time, remains;
str = 'ygjyjgyjgj myemail@gmail.com rjegergje. erertergergrege ergerge wwerttyrtr@gmail.com fthftet ft +1889983453 tyjt tydyj. yutututrt7jyur 12.10.2017. rtyrtyrurtuty yujyky gy ugy 17:00 fhfhhfjf drthdh +1889983402 dthrdhrtdh dthdrrhttrdhr.';
var number = document.getElementById(´number´);
var textContent = number.innerHTML;
emailRegex = /([\w\.]+@[\w]+\.[\w\.]+)/gi;
emails = str.match(emailRegex);

phoneRegex = /(\+[\d]+)/gi;
phones = str.match(phoneRegex);

dateRegex = /(\d{2}\.\d{2}\.\d{4})/gi;
dates = str.match(dateRegex);


timeRegex = /(\d{2}:\d{2})/gi;
times = str.match(timeRegex);

console.log(emails);
console.log(phones);
console.log(dates);
console.log(times);
console.log(str.match(/([\w\.]+@[\w]+\.[\w\.]+)[.\s\w]*(\+[\d]+)[.\s\w]*(\d{2}\.\d{2}\.\d{4})[.\s\w]*(\d{2}:\d{2})([.\s\w]*)/gi));

document.write(emails+"<br/>"+phones+"<br/>"+dates+"<br/>"+times);
};
</script>
</body>

</html>


Posted On : 2017-11-17 15:19:28
Satisfied : 1 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185418912
Reply This Thread
up-rate
-1
down-rate
Comments
I opened it in browser and it doesn't work.
profile Aryan Gupta - anyforum.in Aryan Gupta
6  0  0
Posted On :2017-11-25 16:14:42.0
Leave a Comment
Just replaced ´ by ', Please try once again
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1854  18912
Posted On :2017-11-25 16:49:01.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in