String.replace( ) 实现调换位置 复制代码 代码如下: var name= "Doe, John"; name.replace(/(w+)s*,s*(w+)/, "$2 $1"); // 返回:John Doe
String.replace( ) 实现将所有双引号包含的字符替换成中括号包含的字符 复制代码 代码如下: var text = ""JavaScript" 非常强大!"; text.replace(/"([^"]*)"/g, "[$1]"); // 返回:[JavaScript] 非常强大!
String.replace( ) 将所有字符首字母大写 复制代码 代码如下: var text = "a journey of a thousand miles begins with single step."; text.replace(/w+/g, function(word) { return word.substring(0,1).toUpperCase( ) + word.substring(1); }); // 返回:A Journey Of A Thousand Miles Begins With Single Step.