在文字之間插入空格


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script language="javascript">
function space_text(my_text){
var len = my_text.length;
var text = my_text;
var text2 = text.charAt(0);

for(var i = 1; i < len-1; i++) {
text2 += text.charAt(i);
if(text.charCodeAt(i) > 128 && text.charAt(i+1) != " " && text.charAt(i+1) != "\n")
text2 += " ";
}

text2 += text.charAt(len-1);
len = text2.length;

var text3 = text2.charAt(0);
for(var i = 1; i < len-1; i++) {
if(text2.charCodeAt(i) > 128 && text2.charAt(i-1) != " " && text2.charAt(i-1) != "\n") {
text3 += " ";
}
text3 += text2.charAt(i);
}

text3 += text2.charAt(len-1);

return text3;
}

alert(space_text("你好嗎 123 我很好 This is a test"));
</script>