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){
アイコン=アイコン + 1
} それ以外 {
アイコン=アイコン + 2
}
Strre +=温度
} それ以外 {
壊す
}
}
return strre + "..."
}
2。ドメイン名ホストを取得するためのネイティブJavaScript
コードコピーは次のとおりです。
関数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!= mate){
host = match [1];
}
ホストを返します。
}
3.ネイティブJavaScriptはスペースをクリアします
コードコピーは次のとおりです。
string.prototype.trim = function(){
var reextraspace = /^/s*(.*?)/s+$/;
this.replace(reextraspace、 "$ 1")を返します
}
4.ネイティブJavaScriptをすべて交換します
コードコピーは次のとおりです。
string.prototype.replaceall = function(s1、s2){
this.replace(new regexp(s1、 "gm")、s2)を返します
}
5.ネイティブJavaScriptはHTMLタグを逃れます
コードコピーは次のとおりです。
関数htmlencode(テキスト){
text.replace(/&/g、 '&')。置換(// "/g、 '').replace(/</g、 '<')。
}
6.ネイティブJavaScriptはHTMLタグを復元します
コードコピーは次のとおりです。
関数htmldecode(テキスト){
text.replace(/&/g、 '&')
}
7.ネイティブJavaScriptの時間と日付の形式を変換します
コードコピーは次のとおりです。
date.prototype.format = function(formattr){
var str = formattr;
var week = ['day'、 'one'、 'two'、 'three'、 'four'、 'five'、 'six'];
str = str.replace(/yyyy | yyyy/、this.gettlyear());
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());
strを返します
}
8。ネイティブJavaScriptはそれが数値タイプかどうかを決定します
コードコピーは次のとおりです。
function isdigit(value){
var patrn = /^[0-9]*$ /;
if(patrn.exec(value)== null || value == ""){
falseを返します
} それ以外 {
trueを返します
}
}
9.ネイティブJavaScriptでCookie値を設定します
コードコピーは次のとおりです。
function setCookie(名前、値、時間){
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)unescape(arr [2])を返します。
nullを返します
}