利用json获取字符出现次数的代码
人气:0
代码如下:
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>
复制代码 代码如下:
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>
加载全部内容