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.

JavaScript
JavaScript
JavaScript
How to Process Chinese Input in Node.js: Trim, Replace, and Output

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:

在吗?
在!
你好?
你好!
能听懂汉语吗?
能听懂汉语!
真的吗?
真的!
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptNode.jsencodingChinese text processingstdin
JavaScript
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.