每天一篇javascript學習小結(String對象)_javascript技巧

來源:互聯網
上載者:User

1、string對象中可以傳正則的函數介紹

/*  match() 方法可在字串內檢索指定的值,或找到一個或多個Regex的匹配。  該方法類似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字串的位置。  文法  stringObject.match(searchvalue)  stringObject.match(regexp)  searchvalue 必需。規定要檢索的字串值。  regexp 必需。規定要匹配的模式的 RegExp 對象。如果該參數不是 RegExp 對象,則需要首先把它傳遞給 RegExp 建構函式,將其轉換為 RegExp 對象。 */ var text = "cat, bat, sat, fat";  var pattern = /.at/;  var matches = text.match(pattern);  alert(matches.index); //0 alert(matches[0]);  //"cat" alert(pattern.lastIndex); //0 /*  定義和用法  search() 方法用於檢索字串中指定的子字串,或檢索與Regex相匹配的子字串。  stringObject.search(regexp)  regexp   該參數可以是需要在 stringObject 中檢索的子串,也可以是需要檢索的 RegExp 對象。  注釋:要執行忽略大小寫檢索,請追加標誌 i。  傳回值  stringObject 中第一個與 regexp 相匹配的子串的起始位置。  注釋:如果沒有找到任何匹配的子串,則返回 -1。 */ var pos = text.search(/at/); alert(pos); //1  /*  定義和用法  replace() 方法用於在字串中用一些字元替換另一些字元,或替換一個與Regex匹配的子串。  stringObject.replace(regexp/substr,replacement)  regexp/substr   必需。規定子字串或要替換的模式的 RegExp 對象。  請注意,如果該值是一個字串,則將它作為要檢索的直接量文字模式,而不是首先被轉換為 RegExp 對象。  replacement 必需。一個字串值。規定了替換文本或產生替換文本的函數。  傳回值  一個新的字串,是用 replacement 替換了 regexp 的第一次匹配或所有匹配之後得到的。 */ var result = text.replace("at", "ond"); alert(result); //"cond, bat, sat, fat" result = text.replace(/at/g, "ond"); alert(result); //"cond, bond, sond, fond" result = text.replace(/(.at)/g, "word ($1)"); alert(result); //word (cat), word (bat), word (sat), word (fat)  function htmlEscape(text){  return text.replace(/[<>"&]/g, function(match, pos, originalText){  switch(match){   case "<":   return "<";   case ">":   return ">";   case "&":   return "&";   case "\"":   return """;  }    }); }  alert(htmlEscape("<p class=\"greeting\">Hello world!</p>")); //<p class="greeting">Hello world!</p> /*  定義和用法  split() 方法用於把一個字串分割成字串數組。  stringObject.split(separator,howmany)  separator 必需。字串或Regex,從該參數指定的地方分割 stringObject。  howmany 可選。該參數可指定返回的數組的最大長度。如果設定了該參數,返回的子串不會多於這個參數指定的數組。如果沒有設定該參數,整個字串都會被分割,不考慮它的長度。  傳回值  一個字串數組。該數組是通過在 separator 指定的邊界處將字串 stringObject 分割成子串建立的。返回的數組中的字串不包括 separator 自身。  但是,如果 separator 是包含子運算式的Regex,那麼返回的數組中包括與這些子運算式匹配的字串(但不包括與整個Regex匹配的文本)。 */ var colorText = "red,blue,green,yellow"; var colors1 = colorText.split(","); //["red", "blue", "green", "yellow"] var colors2 = colorText.split(",", 2); //["red", "blue"] var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]

2、字串轉成小寫和大寫函數

 var stringValue = "hello world"; alert(stringValue.toLocaleUpperCase()); //"HELLO WORLD" alert(stringValue.toUpperCase()); //"HELLO WORLD" alert(stringValue.toLocaleLowerCase()); //"hello world" alert(stringValue.toLowerCase()); //"hello world"

3、字串對象 new String()

 var stringObject = new String("hello world"); var stringValue = "hello world";  alert(typeof stringObject); //"object" alert(typeof stringValue); //"string" alert(stringObject instanceof String); //true alert(stringValue instanceof String); //false

4、字串fromCharCode()方法

/*  定義和用法  fromCharCode() 可接受一個指定的 Unicode 值,然後返回一個字串。  String.fromCharCode(numX,numX,...,numX)  numX 必需。一個或多個 Unicode 值,即要建立的字串中的字元的 Unicode 編碼。 */ alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"

5、字串本地比較方法localeCompare()

/*  定義和用法  用本地特定的順序來比較兩個字串。  stringObject.localeCompare(target)  target 要以本地特定的順序與 stringObject 進行比較的字串。  傳回值  說明比較結果的數字。如果 stringObject 小於 target,則 localeCompare() 返回小於 0 的數。  如果 stringObject 大於 target,則該方法返回大於 0 的數。如果兩個字串相等,或根據本地定序沒有區別,該方法返回 0。 */ var stringValue = "yellow";  alert(stringValue.localeCompare("brick")); //1 alert(stringValue.localeCompare("yellow")); //0 alert(stringValue.localeCompare("zoo")); //-1  //preferred technique for using localeCompare() function determineOrder(value) {  var result = stringValue.localeCompare(value);  if (result < 0){  alert("The string 'yellow' comes before the string '" + value + "'.");  } else if (result > 0) {  alert("The string 'yellow' comes after the string '" + value + "'.");  } else {  alert("The string 'yellow' is equal to the string '" + value + "'.");  } }  determineOrder("brick"); determineOrder("yellow"); determineOrder("zoo");

6、indexOf() 和 lastIndexOf()方法

/*  定義和用法  indexOf() 方法可返回某個指定的字串值在字串中首次出現的位置。  stringObject.indexOf(searchvalue,fromindex)  searchvalue 必需。規定需檢索的字串值。  fromindex 可選的整數參數。規定在字串中開始檢索的位置。  它的合法取值是 0 到 stringObject.length - 1。如省略該參數,則將從字串的首字元開始檢索。    定義和用法  lastIndexOf() 方法可返回一個指定的字串值最後出現的位置,在一個字串中的指定位置從後向前搜尋。  stringObject.lastIndexOf(searchvalue,fromindex)  searchvalue 必需。規定需檢索的字串值。  fromindex 可選的整數參數。規定在字串中開始檢索的位置。  它的合法取值是 0 到 stringObject.length - 1。如省略該參數,則將從字串的最後一個字元處開始檢索。 */ var stringValue = "hello world"; alert(stringValue.indexOf("o"));  //4 alert(stringValue.lastIndexOf("o")); //7 alert(stringValue.indexOf("o", 6));  //7 alert(stringValue.lastIndexOf("o", 6)); //4 

7、字串常用字元截取方法

/*  定義和用法  slice() 方法可從已有的數組中返回選定的元素。  arrayObject.slice(start,end)  start 必需。規定從何處開始選取。如果是負數,那麼它規定從數組尾部開始算起的位置。也就是說,-1 指最後一個元素,-2 指倒數第二個元素,以此類推。  end 可選。規定從何處結束選取。該參數是數組片斷結束處的數組下標。如果沒有指定該參數,那麼切分的數組包含從 start 到數組結束的所有元素。  如果這個參數是負數,那麼它規定的是從數組尾部開始算起的元素。  傳回值  返回一個新的數組,包含從 start 到 end (不包括該元素)的 arrayObject 中的元素。  說明  請注意,該方法並不會修改數組,而是返回一個子數組。如果想刪除數組中的一段元素,應該使用方法 Array.splice()。      定義和用法  substring() 方法用於提取字串中介於兩個指定下標之間的字元。  文法  stringObject.substring(start,stop)  start 必需。一個非負的整數,規定要提取的子串的第一個字元在 stringObject 中的位置。  stop 可選。一個非負的整數,比要提取的子串的最後一個字元在 stringObject 中的位置多 1。如果省略該參數,那麼返回的子串會一直到字串的結尾。  傳回值  一個新的字串,該字串值包含 stringObject 的一個子字串,其內容是從 start 處到 stop-1 處的所有字元,其長度為 stop 減 start。  說明  substring() 方法返回的子串包括 start 處的字元,但不包括 stop 處的字元。  如果參數 start 與 stop 相等,那麼該方法返回的就是一個空串(即長度為 0 的字串)。  如果 start 比 stop 大,那麼該方法在提取子串之前會先交換這兩個參數。      定義和用法  substr() 方法可在字串中抽取從 start 下標開始的指定數目的字元。  文法  stringObject.substr(start,length)  start 必需。要抽取的子串的起始下標。必須是數值。如果是負數,那麼該參數聲明從字串的尾部開始算起的位置。   也就是說,-1 指字串中最後一個字元,-2 指倒數第二個字元,以此類推。  length 可選。子串中的字元數。必須是數值。如果省略了該參數,那麼返回從 stringObject 的開始位置到結尾的字串。  傳回值  一個新的字串,包含從 stringObject 的 start(包括 start 所指的字元) 處開始的 length 個字元。  如果沒有指定 length,那麼返回的字串包含從 start 到 stringObject 的結尾的字元。     */ var stringValue = "hello world"; alert(stringValue.slice(3)); //"lo world" alert(stringValue.substring(3)); //"lo world" alert(stringValue.substr(3)); //"lo world" alert(stringValue.slice(3, 7)); //"lo w" alert(stringValue.substring(3,7)); //"lo w" alert(stringValue.substr(3, 7)); //"lo worl"  alert(stringValue.slice(-3));  //"rld" alert(stringValue.substring(-3)); //"hello world" alert(stringValue.substr(-3)); //"rld" alert(stringValue.slice(3, -4)); //"lo w" alert(stringValue.substring(3, -4)); //"hel" alert(stringValue.substr(3, -4)); //"" (empty string)

以上就是今天的javascript學習小結,之後每天還會繼續更新,希望大家繼續關注。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.