Created
July 7, 2015 07:21
-
-
Save simonmysun/5ee7b63453575ca7d280 to your computer and use it in GitHub Desktop.
Add space between Chinese charactors and numbers and letters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: | |
// `node /path/to/addBlank.js /path/to/inputfilename` | |
// `node /path/to/addBlank.js /path/to/inputfilename > /path/to/outputfilename` | |
// | |
// Forked from https://addons.mozilla.org/EN-us/firefox/addon/中文格式/ | |
var fs = require("fs"); | |
var text = fs.readFileSync(process.argv[2],"utf-8"); | |
var newText = "" | |
var lastType = 0; | |
for (var i = 0; i < text.length; i ++) { | |
var char = text.charAt(i); | |
if ((char >= '\u4E00' && char <= '\u9FA5') | |
|| (char >= '\uFE30' && char <= '\uFFA0' && false)) { // don't insert space between punctuations and numbers and letters | |
if (lastType == -1) { | |
newText += ' '; | |
} | |
lastType = 1; | |
} else if ((char >= '0' && char <= '9') | |
|| (char >= 'A' && char <= 'Z') | |
|| (char >= 'a' && char <= 'z')) { | |
if (lastType == 1) { | |
newText += ' '; | |
} | |
lastType = -1; | |
} else { | |
lastType = 0; | |
} | |
newText += char; | |
} | |
console.log(newText); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment