编辑代码

//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
let str = 'hello'
typeof str  // 'string'
let strObj = new String ('hello')
typeof strObj // 'object'

strObj  instanceof  Object  // true 
strObj instanceof String  // true

str.valueOf() // 'hello'
str.toString() // 'hello'
str.length // 5

// 字符串操作方法,不改变原字符串
str.concat(' world') // 'hello world'  连接字符串
console.log (str.slice(0,3))  
/* 'hel' 开始位置,结束位置(即该位置之前的字符会被提取出来)  结束位置为空,默认提取到字符串末尾,slice()方法将所有负值参数都当成字
符串长度加上负参数值*/
console.log (str.substring()) 
 /* 'hello' 开始位置,结束位置(即该位置之前的字符会被提取出来)  结束位置为空,默认提取到字符串末尾,
 substring()方法会将所有负参数值都转换为 0 */
console.log (str.substr(2,2))
 /* 'll' 开始位置,提取字符串数量  提取字符串数量为空,默认提取到字符串末尾,substr()方法将第一个负参数值当成字符串长度加上该值,
 将第二个负参数值转换为 0。*/

 // 字符串 子串位置判断

console.log(str.indexOf('o',2)) // 4  查找子串,从前往后开始查找的位置,不存在返回 -1
console.log(str.lastIndexOf('o',4)) // 4  查找子串,从后往前开始查找的位置,不存在返回 -1

// 是否包含子串
str.startsWith('llo') // false 从开头查找 /^[ll0]/
str.endsWith('llo') // true  从结尾查找 /[llo]$/
str.includes('llo') // true  查找整个字符串 /llo/

str.trim() // 去除前后空格
str.trimLeft() // 去除前空格
str.trimRight() // 去除后空格
str.repeat(1) // 重复字符串
str.padStart(8,'xx') // 复制并填充字符串
str.padEnd(8,'xx') // 复制并填充字符串

str.toLocaleLowerCase() //转小写
str.toLocaleUpperCase() //转大写
str.toLowerCase() //转小写
str.toUpperCase() //转大写

// 字符串模式匹配

console.log(str.match(/o/g)) // ['0'] 返回数组
// match()方法返回的数组与 RegExp 对象的 exec()方法返回的数组是一样的:

str.search(/o/) // 返回匹配字符串索引,未找到返回 -1

str.replace('h','H') // 'Hello'
console.log( str.replace('l','x')) // 'hexlo'
console.log(str.replace(/l/g,'x') )// 'hexxo'

// str.replaceAll('l','x') // 'hexxo

let arr = [
    {color:'red',sort:1},
    {color:'yellow',sort:8},
    {color:'blue',sort:2},
    {color:'red',sort:9},
    {color:'yellow',sort:9},
    {color:'red',sort:9},
]
arr.sort((a,b)=>{
    if(a.sort===b.sort){
        if(a.color>b.color){
            return 1
        }else if(a.color===b.color){
            return 0
        }else{
            return -1
        }
    }else if (a.sort > b.sort) {
        return  0
    }else{
        return -1
    }
})
console.log(arr)