-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring-prototypes.js
72 lines (55 loc) · 2.48 KB
/
string-prototypes.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
String.prototype.linkify = function(options) {
var options = options || {};
options.target = options.target || '_self';
// http://, https://, ftp://
var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
// www. sans http:// or https://
var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
// Email addresses
var emailAddressPattern = /[\w.]+@[a-zA-Z_-]+?(?:\.[a-zA-Z]{2,6})+/gim;
return this
.replace(urlPattern, '<a target="'+options.target+'" href="$&">$&</a>')
.replace(pseudoUrlPattern, '$1<a target="'+options.target+'" href="http://$2">$2</a>')
.replace(emailAddressPattern, '<a target="'+options.target+'" href="mailto:$&">$&</a>');
};
String.prototype.toTileCase = function () {
return this.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
};
String.prototype.isEmail = function () {
var email=this;
var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
var check=/@[\w\-]+\./;
var checkend=/\.[a-zA-Z]{2,3}$/;
return !(((email.search(exclude) != -1) || (email.search(check)) == -1) || (email.search(checkend) == -1));
};
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ucwords = function () {
// example 1: ucwords('kevin van zonneveld');
// returns 1: 'Kevin Van Zonneveld'
// example 2: ucwords('HELLO WORLD');
// returns 2: 'HELLO WORLD'
var str = this;
return this.replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function($1) {
return $1.toUpperCase();
});
};
String.prototype.createSlug = function () {
var str = this;
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
} ;
String.prototype.replaceAll = function(str1, str2, ignore){
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
};