1、string對象中可以傳正則的函數介紹
/*  match() 方法可在字符串內檢索指定的值,或找到一個或多個正則表達式的匹配。  該方法類似 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() 方法用于檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串。  stringObject.search(regexp)  regexp   該參數可以是需要在 stringObject 中檢索的子串,也可以是需要檢索的 RegExp 對象。  注釋:要執行忽略大小寫的檢索,請追加標志 i。  返回值  stringObject 中第一個與 regexp 相匹配的子串的起始位置。  注釋:如果沒有找到任何匹配的子串,則返回 -1。 */ var pos = text.search(/at/); alert(pos); //1  /*  定義和用法  replace() 方法用于在字符串中用一些字符替換另一些字符,或替換一個與正則表達式匹配的子串。  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 必需。字符串或正則表達式,從該參數指定的地方分割 stringObject。  howmany 可選。該參數可指定返回的數組的最大長度。如果設置了該參數,返回的子串不會多于這個參數指定的數組。如果沒有設置該參數,整個字符串都會被分割,不考慮它的長度。  返回值  一個字符串數組。該數組是通過在 separator 指定的邊界處將字符串 stringObject 分割成子串創建的。返回的數組中的字串不包括 separator 自身。  但是,如果 separator 是包含子表達式的正則表達式,那么返回的數組中包括與這些子表達式匹配的字串(但不包括與整個正則表達式匹配的文本)。 */ 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); //false4、字符串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學習小結,之后每天還會繼續更新,希望大家繼續關注。
新聞熱點
疑難解答