Last active
March 21, 2023 15:07
-
-
Save durgesh97025/c6dd62a58f79b949d9b8c8e7542b6bfb to your computer and use it in GitHub Desktop.
Utility for Array, String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.replaceAll = function(search, replacement) { | |
var target = this; | |
return target.replace(new RegExp(search, 'g'), replacement); | |
}; | |
Array.prototype.contains = function(v) { | |
for(var i = 0; i < this.length; i++) { | |
if(this[i] === v) return true; | |
} | |
return false; | |
}; | |
Array.prototype.unique = function() { | |
var arr = []; | |
for(var i = 0; i < this.length; i++) { | |
if(!arr.contains(this[i])) { | |
arr.push(this[i]); | |
} | |
} | |
return arr; | |
} | |
function getQueryStrings() | |
{ | |
var vars = [], hash; | |
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); | |
for(var i = 0; i < hashes.length; i++) | |
{ | |
hash = hashes[i].split('='); | |
vars.push(hash[0]); | |
vars[hash[0]] = hash[1]; | |
} | |
return vars; | |
} | |
Array.prototype.removeByValue = function (val) { | |
for (var i = 0; i < this.length; i++) { | |
if (this[i] == val) { | |
this.splice(i, 1); | |
break; | |
} | |
} | |
} | |
String.prototype.contains = function (it) { return this.indexOf(it) != -1; }; | |
$.fn.noSpecialChar = function () { | |
(this).bind('keypress', function (event) { | |
var regex = new RegExp("^[a-zA-Z0-9]+$"); | |
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); | |
if (!regex.test(key)) { | |
event.preventDefault(); | |
return false; | |
} | |
}); | |
} | |
jQuery.fn.outerhtml = function() { | |
return jQuery('<div />').append(this.eq(0).clone()).html(); | |
}; | |
$.fn.noSpecialChar = function () { | |
(this).bind('keypress', function (event) { | |
var regex = new RegExp("^[a-zA-Z0-9 ]+$"); | |
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); | |
if (!regex.test(key)) { | |
event.preventDefault(); | |
return false; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment