i want to replace the "." into tokens like [dot] from below input to below output
input
this is a test.for a question. at stackoverflow.com the best place to learn. programming. test www.wikipedia.com
output
this is a test.for a question. at stackoverflow[dot]com the best place to learn. programming. test www[dot]wikipedia[dot]com
problems
test.for
we cant use good regex like /[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}/gi
i think its best for using something like below betterfound = string.match(/([a-zA-Z0-9]+\.(com|co\.cc)|more\.domains)/gi);
this work great, i have a problem to join/replace them to the original string. any workarounds like how can we filter elements in array with regex in array with javascript?
how would you tackle this problem? btw im using nodejs other language is acceptable.
thanks
This handles www.example.com
correctly:
tld = ["com", "org", "edu", "net"] // feel free to add more
var input = "this is a test.for a question. at www.stackoverflow.com "
+ "the best place to learn. "
+ "programming.test wikipedia.com and windows.microsoft.edu";
re = new RegExp('\\S+\\.(' + tld.join('|') + ')\\b', 'g')
var dotted = input.replace(re, function($0) {
return $0.replace(/\./g, "[dot]");
});
// this is a test.for a question. at www[dot]stackoverflow[dot]com the best place to learn.
// programming.test wikipedia[dot]com and windows[dot]microsoft[dot]edu
var input = "this is a test.for a question. at stackoverflow.com the best place to learn. programming. test wikipedia.com";
var dotted = input.replace(/(\S+)\.(com|org|edu|net)\b/gi, '$1[dot]$2');
// "this is a test.for a question. at stackoverflow[dot]com the best place to learn. programming. test wikipedia[dot]com"