1、原生javascript实现字符串长度截取
function cutstr(str, len) {
var temp;
var icount = 0;
var patrn = /[^\x00-\xff]/;
var strre = "";
for (var i = 0; i < str.length; i++) {
if (icount < len - 1) {
temp = str.substr(i, 1);
if (patrn.exec(temp) == null) {
icount = icount + 1
} else {
icount = icount + 2
}
strre += temp
} else {
break
}
}
return strre + "..."
}
2、原生javascript获取域名主机
function gethost(url) {
var host = "null";
if(typeof url == "undefined"|| null == url) {
url = window.location.href;
}
var regex = /^\w+\:\/\/([^\/]*).*/;
var match = url.match(regex);
if(typeof match != "undefined" && null != match) {
host = match[1];
}
return host;
}
3、原生javascript清除空格
string.prototype.trim = function() {
var reextraspace = /^\s*(.*?)\s+$/;
return this.replace(reextraspace, "$1")
}
4、原生javascript替换全部
string.prototype.replaceall = function(s1, s2) {
return this.replace(new regexp(s1, "gm"), s2)
}
5、原生javascript转义html标签
function htmlencode(text) {
return text.replace(/&/g, '&').replace(/\"/g, '"').replace(//g, '>')
}
6、原生javascript还原html标签
function htmldecode(text) {
return text.replace(/&/g, '&').replace(/"/g, '\"').replace(//g, '>')
}
7、原生javascript时间日期格式转换
date.prototype.format = function(formatstr) {
var str = formatstr;
var week = ['日', '一', '二', '三', '四', '五', '六'];
str = str.replace(/yyyy|yyyy/, this.getfullyear());
str = str.replace(/yy|yy/, (this.getyear() % 100) > 9 ? (this.getyear() % 100).tostring() : '0' + (this.getyear() % 100));
str = str.replace(/mm/, (this.getmonth() + 1) > 9 ? (this.getmonth() + 1).tostring() : '0' + (this.getmonth() + 1));
str = str.replace(/m/g, (this.getmonth() + 1));
str = str.replace(/w|w/g, week[this.getday()]);
str = str.replace(/dd|dd/, this.getdate() > 9 ? this.getdate().tostring() : '0' + this.getdate());
str = str.replace(/d|d/g, this.getdate());
str = str.replace(/hh|hh/, this.gethours() > 9 ? this.gethours().tostring() : '0' + this.gethours());
str = str.replace(/h|h/g, this.gethours());
str = str.replace(/mm/, this.getminutes() > 9 ? this.getminutes().tostring() : '0' + this.getminutes());
str = str.replace(/m/g, this.getminutes());
str = str.replace(/ss|ss/, this.getseconds() > 9 ? this.getseconds().tostring() : '0' + this.getseconds());
str = str.replace(/s|s/g, this.getseconds());
return str
}
8、原生javascript判断是否为数字类型
function isdigit(value) {
var patrn = /^[0-9]*$/;
if (patrn.exec(value) == null || value == "") {
return false
} else {
return true
}
}
9、原生javascript设置cookie值
function setcookie(name, value, hours) {
var d = new date();
var offset = 8;
var utc = d.gettime() + (d.gettimezoneoffset() * 60000);
var nd = utc + (3600000 * offset);
var exp = new date(nd);
exp.settime(exp.gettime() + hours * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.togmtstring() + ";domain=360doc.com;"
}
10、原生javascript获取cookie值
function getcookie(name) {
var arr = document.cookie.match(new regexp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]);
return null
}
以上就是分享10个原生js的实例代码的详细内容。