-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlExtractor.js
More file actions
30 lines (23 loc) · 854 Bytes
/
urlExtractor.js
File metadata and controls
30 lines (23 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function extractUrls(text) {
var words = text
.replace(/([\s\(\)\[\]<>"'])/g, "\0$1\0")
.replace(/([?;:,.!]+)(?=(\0|$|\s))/g, "\0$1\0")
.split("\0");
var validUrls = [];
words.forEach(function (word) {
if (!word)
return;
var urlRegex = /(ftp: \/\/|www\.|https?:\/\/){1}[a-zA-Z0-9u00a1-\uffff0-]{2,}\.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S*)/g;
if (urlRegex.test(word))
return validUrls.push(word);
var splited = word.split('.');
var isValidUrl = (splited.length > 1 &&
(splited[splited.length - 1].length >= 2 ||
(splited.length > 2 &&
splited[splited.length - 1].length == 0)))
if (isValidUrl)
return validUrls.push(word);
});
return validUrls;
}
module.exports = extractUrls;