How to Process Chinese Input in Node.js: Trim, Replace, and Output
This guide demonstrates how to configure Node.js stdin for UTF-8, read incoming Chinese text, remove question particles and punctuation, replace question marks with exclamation marks, and output the transformed strings, with clear code examples and expected results.
This article shows how to handle Chinese input in a Node.js program by setting the standard input encoding to UTF-8, listening for data events, trimming whitespace, removing the question particle "吗", and converting question marks to exclamation marks before printing the result.
process.stdin.setEncoding("utf-8"); process.stdin.on("data", input =>
console.log(
input.trim()
.replace(/吗/, "")
.replace(/\?|?/g, "!")
)
);Example outputs for various input strings are:
在吗?
在!
你好?
你好!
能听懂汉语吗?
能听懂汉语!
真的吗?
真的!Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
