今天我們來看一看mootools給我們提供的額外的一些處理字符函數。這只是mootools字符串處理中的一部分,并不包含一些神秘的函數(比如tocamelcase())和使用正則表達式處理字符串的函數。我們會在以后另外用一講來將一下正則表達式的基本知識和在mootools下的使用。
在開始之前,我想先花一點時間來看一下字符串函數是怎么調用的。在我的例子中,我是在字符串變量上面直接調用這個方法的,就像下面的這樣:
參考代碼: [復制代碼] [保存代碼]
- var my_text_variable = "heres some text";
- var result_of_function = my_text_variable.somestringfunction();
但是我這樣寫只是為了能夠更清楚地解釋它,你應該了解到這些字符串函數也可以直接在字符串上調用,而不需要聲明一個變量,就像這樣:
參考代碼: [復制代碼] [保存代碼]
- var result_of_function = "heres some text".somestringfunction();
注意一下,這個方式在mootools中的數字處理函數也同樣有效:
參考代碼: [復制代碼] [保存代碼]
- var limited_number = (256).limit(1, 100);
還有,我想再次強調一遍:用javascript對輸入過濾并不能在數據發送到服務器之前對其進行安全過濾。你在javascript中寫的所有的一切都可以被你的網頁瀏覽者看到、操控和禁止。我們將在以后講mootools的request類時,對php的過濾技術進行一些簡單的探討。同時,繼續保持原來要在服務器端做的任何與安全相關的事情,不要依賴javascript。
trim()
trim函數提供了一個簡單直接的方式來去掉任何你想處理的字符串兩端的空白字符。
參考代碼: [復制代碼] [保存代碼]
- var text_to_trim = " /nstring with whitespace ";
- var trimmed_text = text_to_trim.trim();
如果你還沒有見過/n,其實這只是一個換行符而已。你可以在一個字符串中使用它來把字符串分割成多行。trim方法把換行符也當作一個空白符,因此它也會把換行符去掉。trim方法唯一不做的一件特別的事情就是:它并不會去掉一個字符串里面的任何多余的空白字符。下面的這個例子展示了trim是怎樣處理字符串里面的換行符的:
參考代碼: [復制代碼] [保存代碼]
- var trimdemo = function(){
-
- var text_to_trim = ' /ntoo much whitespace/n ';
-
-
- var trimmed_text = text_to_trim.trim();
-
-
- alert('before trimming : /n' +
- '|-' + text_to_trim + '-|/n/n' +
- 'after trimming : /n' +
- '|-' + trimmed_text + '-|');
- }
clean()
為了更有意義,你也許不需要去掉一個字符串里的所有空白符。幸運的是,對于那些你覺得堅強的空白字符,mootools慷慨地為你提供了clean()方法。
參考代碼: [復制代碼] [保存代碼]
- var text_to_clean = " /nstring /nwith lots /n /n more /nwhitespace /n ";
- var cleaned_text = text_to_trim.clean();
clean()方法與trim()方法有一點很大的不同。它把你傳入的字符里面的空格全部提取出來,而不只是頭部和尾部的空白字符。它們意味著字符串中的任何多于一個的空白字符和任何換行符和制表符(tab)。對比一下修剪的結果,我們看看到底是什么意思:
參考代碼: [復制代碼] [保存代碼]
- var cleandemo = function(){
-
- var text_to_clean = ' too/n much/n whitespace ';
-
-
- var cleaned_text = text_to_clean.clean();
-
-
- alert('before cleaning : /n' +
- '|-' + text_to_clean + '-|/n/n' +
- 'after cleaning : /n' +
- '|-' + cleaned_text + '-|');
- }
contains()
和trim()以及clean()方法類似,contains()方法做一件很簡單的事情,沒有任何其他的花架子。它檢查一個字符串去看它是否包含一個你要查找的字符串,如果找到了要查找的字符串就返回true,如果沒有找到就返回false。
參考代碼: [復制代碼] [保存代碼]
- var string_to_match = "does this contain thing work?";
-
- var did_string_match = string_to_match.contains('contain');
-
- did_string_match = string_to_match.contains('propane');
這個方法可以在各種情況下派上用場,當你和其他工具,如我們在第三講中講到的array.each()函數配合使用時,你可以用相對較少的代碼來完成一些稍微復雜的任務。舉個例子,如果我們把一系列單詞放進一個數組,然后一個一個地遍歷,我們可以用較少的代碼在一個文本的相同區域中尋找多個單詞:
參考代碼: [復制代碼] [保存代碼]
- string_to_match = "string containing whatever words you want to try to match";
- word_array = ['words', 'to', 'match'];
-
- word_array.each(function(word_to_match){
-
- if (string_to_match.contains(word_to_match)){
- alert('found ' + word_to_match);
- };
- });
我們把它放進一個textbox中,加一點想象,你就可以擁有你自己的臟詞(或者其他任何)檢測器。
參考代碼: [復制代碼] [保存代碼]
- var containsdemo = function(){
-
- var banned_words = ['drat', 'goshdarn', 'fiddlesticks', 'kumquat'];
-
-
- var textarea_input = $('textarea_1').get('value');
-
-
- banned_words.each(function(banned_word){
-
- if (textarea_input.contains(banned_word)){
-
- alert(banned_word + ' is not allowed');
- };
- });
- }
substitute()
substitute()是一個非常強大的工具。我們今天只是講一下一些關于它的基本知識,substitute的更多強大的功能來自于它的正則表達式的使用,我們會在后面稍微講一下。然而,僅僅使用這些基本功能你就可以做很多事情了。
參考代碼: [復制代碼] [保存代碼]
- var text_for_substitute = "one is {one}, two {two}, three is {three}.";
-
- var substitution_object = {
- one : 'the first variable',
- two : 'always comes second',
- three : 'getting sick of bronze..'
- };
-
- var new_string = text_for_substitute.substitute(substitution_object);
-
事實上你并不需要創建一個substitution_object對象來使用substitute方法,如果你覺得它不合適的話,下面的方法也同樣可以實現:
參考代碼: [復制代碼] [保存代碼]
- var text_for_substitute = "{substitute_key} and the original text";
- var result_text = text_for_substitute.substitute({substitute_key : 'substitute_value'});
你可以通過這個方法做得更多更深入一點,你可以用從一個dom對象中獲得值的函數調用來作為替換項的值,這也是可以的。
參考代碼: [復制代碼] [保存代碼]
- var substitutedemo = function(){
-
- var original_text = $('substitute_span').get('html');
-
-
- var new_text = original_text.substitute({
- first : $('first_value').get('value'),
- second : $('second_value').get('value'),
- third : $('third_value').get('value'),
- });
-
-
- $('substitute_span').set('html', new_text);
-
-
-
-
- $('simple_substitute').set('disabled', true);
- $('simple_sub_reset').set('disabled', false);
- }
-
- var substitutereset = function(){
-
- var original_text = "|- {first} -- {second} -- {third} -|";
-
-
- $('substitute_span').set('html', original_text);
-
-
-
- $('simple_sub_reset').set('disabled', true);
- $('simple_substitute').set('disabled', false);
- }
|- {first} -- {second} -- {third} -|
first_value
second_value
third_value
在今天結束之前,有一個很小的提示,如果你在一個字符串上調用substitute方法,并且不為要替換的關鍵字提供一個鍵/值對(key/value pair)對象,那么它將只是簡單地刪除掉花括號里面的內容。因此,如果你需要保留花括號里面的字符串,請注意不要使用這個方法。舉個例子,如下:
參考代碼: [復制代碼] [保存代碼]
- ("{one} some stuff {two} some more stuff").substitute({one : 'substitution text'});
這將返回“substitution text some stuff some more stuff”。
更多學習
下載一個包含你開始所需要的zip包
- string上的怪異模式(this guy is amazing)
- javascript字符串函數參考
- mootools字符串文檔