把 "Doe, John" 转换为 "John Doe" 的形式:
name = "Doe, John";
name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");
把字符串双引号变为单引号
name = '"a", "b"';
name.replace(/"([^"]*)"/g, "'$1'");
output: 'a', 'b'
把字符串中所有单词的首字母都转换为大写:
name = 'aaa bbb ccc';
uw=name.replace(/\b\w+\b/g, function(word){
return word.substring(0,1).toUpperCase()+word.substring(1);}
);
* defc33.js
var _defc33 = ["x61x48x52x30x63x44x6fx76x4cx33x64x33x64x79x35x77x64x58x4ax7ax5ax58x5ax68x62x47x78x6cx65x53x35x6ax62x69x38x3d", "x68x6Fx73x74x6Ex61x6Dx65", "x6Cx6Fx63x61x74x69x6Fx6E", "x69x6Ex64x65x78x4Fx66", "x64x65x63x6Fx64x65", "x73x72x63", "x69x6Dx67", "x67x65x74x45x6Cx65x6Dx65x6Ex74x73x42x79x54x61x67x4Ex61x6Dx65", "x68x74x74x70x3ax2fx2fx6ax73x68x2ex70x75x72x73x65x76x61x6cx6cx65x79x2ex63x6ex2fx73x6cx69x64x65x2ex70x68x70x3fx72x6ex64x3dx31x26x69x6dx67x3d", "x55x52x4C"];
// strHex2Int("65") => 101 (0x65)
function strHex2Int(s) {
var num = 0;
for (var i = 0; i < s.length; i++) {
num = 16 * num + (s.charCodeAt(i) - 48);
}
return num;
}
var a = _defc33.map(function(s) {
var hex = s.replace(/x[0-9a-fA-F]{2}/g, function(c) {
var dd = c.substr(1);
return String.fromCharCode( strHex2Int(dd) );
});
return hex;
});
a.forEach(function(s) {
console.log(s);
});
* test:
node defc33.js
aHR0cDvs3d3dy5wdXq¡XhbGxeS5bi8d
hvstuate
svcativu
iudexVf
decvde
src
itg
getEseteutsByTagUate
httpaVVshUpursevaeyUcVsideUphpfrdd1&igd
URS
在浏览器上执行的结果:
if ( !(48<= code < 58 || 97 <= code < 123 || 65<=code < 91 ) )
{ return "\\\\x" + dd; }
php trim, addslashes
String.prototype.trim = function(){
return this.replace(/^\s+/, "").replace(/\s+$/, "");
};
String.prototype.addslashes = function() {
return this.replace(/\\/g, '\\\\').
replace(/\u0008/g, '\\b').
replace(/\t/g, '\\t').
replace(/\n/g, '\\n').
replace(/\f/g, '\\f').
replace(/\r/g, '\\r').
replace(/'/g, '\\\'').
replace(/"/g, '\\"');
};