-
Notifications
You must be signed in to change notification settings - Fork 0
/
mentionerjs.js
33 lines (31 loc) · 1018 Bytes
/
mentionerjs.js
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
31
32
33
// ======================================================
//
// ©2024 - MENTIONERJS - 1.0
// Mention @users, create #hashtags and remove
// http(s)/www from URLs easily - Vanilla JavaScript
//
// byjaris.com @byJaris | jarisgv.com @JarisGV
//
// ======================================================
var parserRules = [
{
pattern: /(?<!\b)@([A-Za-z0-9._\-]+.*?)/g,
replacement: "<a href='//twitter.com/$1' target='_blank'>@$1</a>"
},
{
pattern: /(?<!\b)#([A-Za-z0-9._\-]+.*?)/g,
replacement:
"<a href='//instagram.com/explore/tags/$1' target='_blank'>#$1</a>"
},
{
pattern: /(?:http:\/\/|(?<!\s)http:\/\/www\.|https:\/\/|https:\/\/www\.|www\.)+([A-Za-z0-9\/._\-]+.*?)/g,
replacement: "<a href='https://$1' target='_blank'>$1</a>"
}
];
document.querySelectorAll(".main, #preview").forEach(function (tag) {
var inner = tag.innerHTML;
parserRules.forEach(function (rule) {
inner = inner.replace(rule.pattern, rule.replacement);
});
tag.innerHTML = inner;
});