Skip to content

Instantly share code, notes, and snippets.

@chinghanho
Last active August 29, 2015 13:56
Show Gist options
  • Save chinghanho/9208318 to your computer and use it in GitHub Desktop.
Save chinghanho/9208318 to your computer and use it in GitHub Desktop.
尋找中英文夾雜 string 中的 URL
var url = /^(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?:\w+:\w+@)?((?:(?:[-\w\d{1-3}]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|edu|co\.uk|ac\.uk|it|fr|tv|museum|asia|local|travel|[a-z]{2}))|((\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)(\.(\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)){3}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$ |\/.,*:;=]|%[a-f\d]{2})*)?$/i
, chinese = /[\u4e00-\u9fa5]+/
, cText = '中文https://github.com/夾雜網址。'
, eText = 'english https://github.com/ url.'
, mixText = '中 en 文https://github.com/夾雜 google.com網址'
, gResult;
// utilities
var flatten = function(array) {
var result = []
, self = arguments.callee;
array.forEach(function(item) {
Array.prototype.push.apply(
result,
Array.isArray(item) ? self(item) : [item]
);
});
return result;
};
gResult =
mixText
.split(chinese)
.map(function (word) {
return word.split(/\s/);
});
gResult =
flatten(gResult)
.filter(function (word) {
return url.test(word);
});
console.log(gResult);
// => [ 'https://github.com/', 'google.com' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment