/* 定義一個字符串 */ var str = 'sanbubaitou'; /* 將字符串轉換成全大寫 */ var s = str.toUpperCase(); console.log( s );//SANBUBAITOU /* 將字符串轉換成全小寫 */ console.log( s.toLowerCase() );//sanbubaitou
var str = 'sanbubaitou'; /* length屬性表示字符串的長度(字符的個數) */ console.log( str.length );// 顯示 11 /* charAt()方法 */ console.log( str.charAt( 0 ) );// 顯示 s /* charCodeAt()方法 */ console.log( str.charCodeAt( 0 ) );// 顯示 115(Unicode碼)
以上兩種方法只能匹配到第一個與目標字符相同的字符,並顯示位置信息正則表達式
/* 定義一個字符串 */ var str = 'sanbubaitou'; /* 經過indexOf()方法進行檢測 */ console.log( str.indexOf( 'a' ) );// 顯示 1(索引值) /* 經過lastIndexOf()方法進行檢測 */ console.log( str.lastIndexOf( 'u' ) );// 顯示 10(索引值) /* 檢測的目標不存在時 */ console.log( str.indexOf( 'w' ) );// 顯示 -1
該方法的參數數組
/* 定義一個字符串 */ var str = 'sanbubaitou'; /* slice()方法 */ console.log( str.slice( 4, 8 ) );// 顯示 ubai
該方法的參數code
/* 定義一個字符串 */ var str = 'sanbubaitou'; /* substr()方法 */ console.log( str.substr( 5, 10 ) );// 顯示 baitou
該方法的參數索引
/* 定義一個字符串 */ var str = 'sanbubaitou'; /* substring()方法 */ console.log( str.substring( 3, 9 ) );// 顯示 bubait
slice()方法的參數能夠寫負值字符串
substring()方法的參數不能夠寫負數input
/* 定義一個字符串 */ var str = 'sanbubaitou'; /* slice()方法 */ console.log( str.slice( -8, -4 ) );// 顯示 buba /* substring()方法 */ console.log( str.substring( -9, -3 ) );// 沒有顯示
參數 - 字符與字符之間的分隔符string
/* 定義一個字符串 */ var str1 = 's,a,n,b,u,b,a,i,t,o,u'; /* 分隔字符串 - 有分隔符的 */ var s1 = str1.split( ',' ); console.log( s1 );// 顯示 [ 's', 'a', 'n', 'b', 'u', 'b', 'a', 'i', 't', 'o', 'u' ] /* 定義另外一個字符串 */ var str2 = 'sanbubaitou'; /* 分隔字符串 - 沒有分隔符的 */ var s2 = str2.split( '' ); console.log( s2 );// 顯示 [ 's', 'a', 'n', 'b', 'u', 'b', 'a', 'i', 't', 'o', 'u' ]
參數it
該方法具備返回值console
/* 定義一個字符串 */ var str = 'Microsoft is a big Company, microsoft’s color is red and has MICROSOFT logo like microsoft'; /* 進行替換 */ var s1 = str.replace( /microsoft/, '三步白頭' ); console.log( s1 );// 顯示 Microsoft is a big Company, 三步白頭’s color is red and has MICROSOFT logo like microsoft /* 添加修飾符搜索 */ var s2 = str.replace( /microsoft/i, '三步白頭' ); console.log( s2 );// 顯示 三步白頭 is a big Company, microsoft’s color is red and has MICROSOFT logo like microsoft /* 添加修飾符搜索 */ var s3 = str.replace( /microsoft/ig, '三步白頭' ); console.log( s3 );// 顯示 三步白頭 is a big Company, 三步白頭’s color is red and has 三步白頭 logo like 三步白頭
該方法具備返回值 - 一個數組ast
/* 定義一個字符串 */ var str = 'Microsoft is a big Company, microsoft’s color is red and has MICROSOFT logo like microsoft'; /* 進行匹配 */ var s1 = str.match( /microsoft/ ); console.log( s1 );// 顯示 [ 'microsoft', index: 28, input: 'Microsoft is a big Company, microsoft’s color is red and has MICROSOFT logo like microsoft' ] /* 添加修飾符匹配 */ var s2 = str.match( /microsoft/i ); console.log( s2 );// 顯示 [ 'Microsoft', index: 0, input: 'Microsoft is a big Company, microsoft’s color is red and has MICROSOFT logo like microsoft' ] /* 添加修飾符匹配 */ var s3 = str.match( /microsoft/ig ); console.log( s3 );// 顯示 [ 'Microsoft', 'microsoft', 'MICROSOFT', 'microsoft' ]
該方法具備返回值 - 一個數組
/* 定義一個字符串 */ var str = 'Microsoft is a big Company, microsoft’s color is red and has MICROSOFT logo like microsoft'; /* 進行匹配 */ var s1 = str.search( /microsoft/ ); console.log( s1 );// 顯示 28(索引值) /* 添加修飾符匹配 */ var s2 = str.search( /microsoft/i ); console.log( s2 );// 顯示 0(索引值) /* 添加修飾符匹配 */ var s3 = str.search( /microsoft/ig ); console.log( s3 );// 顯示 0(索引值)