一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

Prototype String對象 學習

復制代碼 代碼如下:
//String對象的靜態方法
Object.extend(String, {
interpret: function(value) {
return value == null ? '' : String(value);
},
specialChar: {
'/b': '//b',
'/t': '//t',
'/n': '//n',
'/f': '//f',
'/r': '//r',
'//': '////'
}
});

Object.extend(String.prototype, (function() {

//內部方法,為gsub和sub函數初始化replacement參數
function prepareReplacement(replacement) {
if (Object.isFunction(replacement)) return replacement;
var template = new Template(replacement);
return function(match) { return template.evaluate(match) };
}

//用replacement替換所有符合pattern的字符串
//注意當replacement不為函數時,這里面會有一個遞歸調用,其中參數pattern會變為Template.pattern
//可以參考Template的evaluate方法,這里構造的非常巧妙,在while循環里的else部分會處理這個遞歸調用的結果
function gsub(pattern, replacement) {
var result = '', source = this, match;
replacement = prepareReplacement(replacement);

if (Object.isString(pattern))
pattern = RegExp.escape(pattern);

//如果pattern參數為null或者'',用把整個字符串按照單個字符分割,并且在每個字符的前后添加replacement
if (!(pattern.length || pattern.source)) {
replacement = replacement('');
return replacement + source.split('').join(replacement) + replacement;
}

while (source.length > 0) {
     //如果source匹配pattern
if (match = source.match(pattern)) {
         //取出匹配之前的字符串
result += source.slice(0, match.index);
        //匹配替換
result += String.interpret(replacement(match));
        //把匹配字符之后的部分賦給source,進行下一次匹配
source = source.slice(match.index + match[0].length);
} else { //如果source不匹配pattern,則直接把source添加到結果上,并把source置空,結束循環
result += source, source = '';
}

}
return result;
}

//基本意思和gsub一樣,只不過多一個count參數,表示要替換幾次
function sub(pattern, replacement, count) {
replacement = prepareReplacement(replacement);
count = Object.isUndefined(count) ? 1 : count;

return this.gsub(pattern, function(match) {
if (--count < 0) return match[0];
return replacement(match);
});
}

//對符合pattern的字符串進行iterator調用
function scan(pattern, iterator) {
this.gsub(pattern, iterator);
return String(this);
}

//按照給定長度截斷字符串
function truncate(length, truncation) {
length = length || 30;
truncation = Object.isUndefined(truncation) ? '...' : truncation;
return this.length > length ?
this.slice(0, length - truncation.length) + truncation : String(this);
}

//剔除字符串前后空格
function strip() {
return this.replace(/^/s+/, '').replace(//s+$/, '');
}

//把字符串的html標記剔除
function stripTags() {
return this.replace(/</w+(/s+("[^"]*"|'[^']*'|[^>])+)?>|<///w+>/gi, '');
}

//把字符串中的script標記剔除
function stripScripts() {
return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
}

//獲取字符串中的script內容
function extractScripts() {
var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
return (this.match(matchAll) || []).map(function(scriptTag) {
return (scriptTag.match(matchOne) || ['', ''])[1];
});
}

//執行字符串中的script內容
function evalScripts() {
return this.extractScripts().map(function(script) { return eval(script) });
}

//轉義HTML內容,例如把'<>&'等特殊字符替換成標準的HTML表達形式
function escapeHTML() {
escapeHTML.text.data = this;
return escapeHTML.div.innerHTML;
}

function unescapeHTML() {
var div = document.createElement('div');
div.innerHTML = this.stripTags();
return div.childNodes[0] ? (div.childNodes.length > 1 ?
$A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) :
div.childNodes[0].nodeValue) : '';
}

//按照separator參數把字符串分割成查詢參數形式
function toQueryParams(separator) {
var match = this.strip().match(/([^?#]*)(#.*)?$/);
if (!match) return { };

return match[1].split(separator || '&').inject({ }, function(hash, pair) {
if ((pair = pair.split('='))[0]) {
var key = decodeURIComponent(pair.shift());
var value = pair.length > 1 ? pair.join('=') : pair[0];
if (value != undefined) value = decodeURIComponent(value);

if (key in hash) {
if (!Object.isArray(hash[key])) hash[key] = [hash[key]];
hash[key].push(value);
}
else hash[key] = value;
}
return hash;
});
}

function toArray() {
return this.split('');
}

//返回字符串的字符
function succ() {
return this.slice(0, this.length - 1) +
String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
}

//獲得重復的字符串
function times(count) {
return count < 1 ? '' : new Array(count + 1).join(this);
}

//把css樣式類型的字符串轉換成腳本形式
function camelize() {
var parts = this.split('-'), len = parts.length;
if (len == 1) return parts[0];

var camelized = this.charAt(0) == '-'
? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
: parts[0];

for (var i = 1; i < len; i++)
camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);

return camelized;
}

//首字母大寫
function capitalize() {
return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
}

//'borderBottomWidth'.underscore();
// -> 'border_bottom_width'
function underscore() {
return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z/d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase();
}

//'border_bottom_width'.dasherize();
// -> 'border-bottom-width'
function dasherize() {
return this.gsub(/_/,'-');
}

//Returns a debug-oriented version of the string(返回一個用來調式的字符串)
function inspect(useDoubleQuotes) {
var escapedString = this.gsub(/[/x00-/x1f//]/, function(match) {
var character = String.specialChar[match[0]];
return character ? character : '//u00' + match[0].charCodeAt().toPaddedString(2, 16);
});
if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '//"') + '"';
return "'" + escapedString.replace(/'/g, '///'') + "'";
}

function toJSON() {
return this.inspect(true);
}

function unfilterJSON(filter) {
return this.sub(filter || Prototype.JSONFilter, '#{1}');
}

function isJSON() {
var str = this;
if (str.blank()) return false;
str = this.replace(///./g, '@').replace(/"[^"http:///n/r]*"/g, '');
return (/^[,:{}/[/]0-9./-+Eaeflnr-u /n/r/t]*$/).test(str);
}

//Strips comment delimiters around Ajax JSON or JavaScript responses. This security method is called internally.
function evalJSON(sanitize) {
var json = this.unfilterJSON();
try {
if (!sanitize || json.isJSON()) return eval('(' + json + ')');
} catch (e) { }
throw new SyntaxError('Badly formed JSON string: ' + this.inspect());
}

function include(pattern) {
return this.indexOf(pattern) > -1;
}

function startsWith(pattern) {
return this.indexOf(pattern) === 0;
}

function endsWith(pattern) {
var d = this.length - pattern.length;
return d >= 0 && this.lastIndexOf(pattern) === d;
}

function empty() {
return this == '';
}

function blank() {
return /^/s*$/.test(this);
}

//和Template的evaluate方法一樣
function interpolate(object, pattern) {
return new Template(this, pattern).evaluate(object);
}

return {
gsub: gsub,
sub: sub,
scan: scan,
truncate: truncate,
strip: String.prototype.trim ? String.prototype.trim : strip,
stripTags: stripTags,
stripScripts: stripScripts,
extractScripts: extractScripts,
evalScripts: evalScripts,
escapeHTML: escapeHTML,
unescapeHTML: unescapeHTML,
toQueryParams: toQueryParams,
parseQuery: toQueryParams,
toArray: toArray,
succ: succ,
times: times,
camelize: camelize,
capitalize: capitalize,
underscore: underscore,
dasherize: dasherize,
inspect: inspect,
toJSON: toJSON,
unfilterJSON: unfilterJSON,
isJSON: isJSON,
evalJSON: evalJSON,
include: include,
startsWith: startsWith,
endsWith: endsWith,
empty: empty,
blank: blank,
interpolate: interpolate
};
})());

Object.extend(String.prototype.escapeHTML, {
div: document.createElement('div'),
text: document.createTextNode('')
});

String.prototype.escapeHTML.div.appendChild(String.prototype.escapeHTML.text);
//以下估計是解決瀏覽器兼容問題
if ('</n>'.escapeHTML() !== '</n>') {
String.prototype.escapeHTML = function() {
return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
};
}

if ('</n>'.unescapeHTML() !== '</n>') {
String.prototype.unescapeHTML = function() {
return this.stripTags().replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
};
}

blank
camelize
capitalize
dasherize
empty
endsWith
escapeHTML
evalJSON
evalScripts
extractScripts
gsub
include
inspect
interpolate
isJSON
parseQuery
scan
startsWith
strip
stripScripts
stripTags
sub
succ
times
toArray
toJSON
toQueryParams
truncate
underscore
unescapeHTML
unfilterJSON
下面只給出一些重要方法的例子,簡單方法就略過了

escapeHTML() /unescapeHTML() :
復制代碼 代碼如下:
'<div class="article">This is an article</div>'.escapeHTML();
// -> "<div class="article">This is an article</div>"

'x > 10'.unescapeHTML()
// -> 'x > 10' '<h1>Pride & Prejudice</h1>'.unescapeHTML()
// -> 'Pride & Prejudice'

evalJSON() /evalScripts()

String對象里面的有幾個方法是為了防止XSS Attack攻擊的,有興趣的可以搜一下,下面給出XSS的概念:

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications which allow code injection by malicious web users into the web pages viewed by other users.
復制代碼 代碼如下:
var person = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
person.name;
//-> "Violet"

person = 'grabUserPassword()'.evalJSON(true);
//-> SyntaxError: Badly formed JSON string: 'grabUserPassword()'

person = '/*-secure-/n{"name": "Violet", "occupation": "character"}/n*/'.evalJSON()
person.name;
//-> "Violet"

復制代碼 代碼如下:
'lorem... <script type="text/Javascript"><!--
2 + 2
// --></script>'.evalScripts();
// -> [4]

'<script type="text/Javascript"><!--
2 + 2
// --></script><script type="text/Javascript"><!--
alert("hello world!")
// --></script>'.evalScripts();
// -> [4, undefined] (and displays 'hello world!' in the alert dialog)

gsub() /sub() :
復制代碼 代碼如下:
var mouseEvents = 'click dblclick mousedown mouseup mouseover mousemove mouseout'; mouseEvents.gsub(' ', ', ');
// -> 'click, dblclick, mousedown, mouseup, mouseover, mousemove, mouseout'

mouseEvents.gsub(//w+/, function(match){return 'on' + match[0].capitalize()});
// -> 'onClick onDblclick onMousedown onMouseup onMouseover onMousemove onMouseout'

var markdown = '![a pear](/img/pear.jpg) ![an orange](/img/orange.jpg)';
markdown.gsub(/!/[(.*?)/]/((.*?)/)/, function(match){ return '<img alt="' + match[1] + '" src="' + match[2] + '" src="' + match[2] + '" />'; });
// -> '<img alt="a pear" src="/img/pear.jpg" src="img/pear.jpg" /> <img alt="an orange" src="/img/orange.jpg" src="img/orange.jpg" />'

//==================================================

var fruits = 'apple pear orange';
fruits.sub(' ', ', '); // -> 'apple, pear orange'
fruits.sub(' ', ', ', 1); // -> 'apple, pear orange'
fruits.sub(' ', ', ', 2); // -> 'apple, pear, orange'
fruits.sub(//w+/, function(match){return match[0].capitalize() + ','}, 2);
// -> 'Apple, Pear, orange'

var markdown = '![a pear](/img/pear.jpg) ![an orange](/img/orange.jpg)';
markdown.sub(/!/[(.*?)/]/((.*?)/)/, function(match){ return '<img alt="' + match[1] + '" src="' + match[2] + '" src="' + match[2] + '" />'; });
// -> '<img alt="a pear" src="/img/pear.jpg" src="img/pear.jpg" /> ![an orange](/img/orange.jpg)'

markdown.sub(/!/[(.*?)/]/((.*?)/)/, '<img alt="#{1}" src="#{2}" src="#{2}" />');
// -> '<img alt="a pear" src="/img/pear.jpg" src="img/pear.jpg" /> ![an orange](/img/orange.jpg)'

interpolate() :
復制代碼 代碼如下:
"#{animals} on a #{transport}".interpolate({ animals: "Pigs", transport: "Surfboard" });
//-> "Pigs on a Surfboard"

scan() :
復制代碼 代碼如下:
var fruits = [];
'apple, pear & orange'.scan(//w+/, function(match){ fruits.push(match[0])}); fruits.inspect()
// -> ['apple', 'pear', 'orange']

times() :
復制代碼 代碼如下:
"echo ".times(3); //-> "echo echo echo "

toQueryParams():
復制代碼 代碼如下:
'section=blog&id=45'.toQueryParams();
// -> {section: 'blog', id: '45'}

'section=blog;id=45'.toQueryParams();
// -> {section: 'blog', id: '45'}

'http://www.example.com?section=blog&id=45#comments'.toQueryParams();
// -> {section: 'blog', id: '45'}

'section=blog&tag=Javascript&tag=prototype&tag=doc'.toQueryParams();
// -> {section: 'blog', tag:['Javascript', 'prototype', 'doc']}

'tag=ruby%20on%20rails'.toQueryParams();
// -> {tag: 'ruby on rails'}

'id=45&raw'.toQueryParams();
// -> {id: '45', raw: undefined}

JavaScript技術Prototype String對象 學習,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 美女很黄很黄是免费的·无遮挡网站 | 精品在线观看免费 | 国产久热精品 | 色偷偷91久久综合噜噜噜 | 一区二区中文字幕在线观看 | 色天使色婷婷在线影院亚洲 | 一区二区免费视频 | 中文字幕精品亚洲无线码二区 | 国产久视频 | 欧美人最猛性xxxxx | 九九免费| 91免费高清视频 | 伊人狠狠| 久热网 | 国产一区二区网站 | 另类av | 精品久久久久久久一区二区手机版 | 思思玖玖玖在线精品视频 | 真人视频一级毛片 | avtt亚洲一区中文字幕 | 久久91亚洲精品中文字幕奶水 | 免费国产成人午夜在线观看 | 播放久久国产乱子伦精品 | 伊人99在线 | 日本大片免a费观看视频+播放器 | 中文一区在线观看 | 五月天丁香六月欧美综合 | 久9精品视频 | 国产在线播放网站 | 91麻豆精品激情在线观看最新 | 一区二区三区四区视频在线观看 | 国产婷婷成人久久av免费高清 | 美女视频永久黄网站免费观看韩国 | 国产一区二区视频免费 | 男女一级毛片免费视频看 | 精品国产亚一区二区三区 | 久久青青草原精品国产软件 | 久久久久久穴 | 亚洲成在人天堂一区二区 | 久久亚洲天堂 | 色多多www视频在线观看免费 |