代碼如下:
復制代碼 代碼如下:
function ObjRegExp(){
var newStr= "Drive someone up a wall";
newStr = newStr.replace(new RegExp(/\s/ig),"");
var objJson = {};
for(var i = 0 ;i < newStr.length ; i++) {
var objChar = newStr.charAt(i);
if(objJson[objChar]){
objJson[objChar] = parseInt(objJson[objChar]) + 1;
}else{
objJson[objChar] = 1;
}
}
var oDiv = document.getElementById("objDiv");
oDiv.appendChild(document.createTextNode("Drive someone up a wall!"));
oDiv.appendChild(document.createElement("<br/>"));
for(var key in objJson){
oDiv.appendChild(document.createTextNode(key +":"+ objJson[key]));
oDiv.appendChild(document.createElement("<br/>"));
}
}
Html 如下:
復制代碼 代碼如下:
<body>
<div id="objDiv" style=" margin-top:100px; margin-left:300px;"></div>
<input type="text" id="txtEmail"/>
<input id="btnSubmit" type="button" value="Submit" onclick="ObjRegExp();" />
</body>