SoFunction
Updated on 2025-04-06

Example of JS regular matching Chinese method

This article describes the method of JS regular matching Chinese. Share it for your reference, as follows:

need: Use JS regular method to match and output the Chinese characters in the string "[smile][pray your lips][daily][chilling]".

Sample code:

<script>
var pattern1 = /[\u4e00-\u9fa5]+/g;
var pattern2 = /\[[\u4e00-\u9fa5]+\]/g;
var contents = "[smile][puff the lips][daily][satisfied][cry]";
content = (pattern1);
alert(content);
content = (pattern2);
alert(content);
</script>

Looking at the above content, Chinese character content has been obtained through pattern1 alert content. Then the meaning of pattern2 is to pay attention to points [,]. It’s nothing. It’s a very simple analysis. It’s worth noting that [\u4e00-\u9fa5] is enough to match Chinese.

PS: Here are two very convenient regular expression tools for your reference:

JavaScript regular expression online testing tool:
http://tools./regex/javascript

Regular expression online generation tool:
http://tools./regex/create_reg

For more information about JavaScript, readers who are interested in reading this site's special topic:Complete collection of JavaScript regular expression techniques》、《Summary of JavaScript replacement operation skills》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript traversal algorithm and skills》、《Summary of json operation skills in JavaScript》、《Summary of JavaScript Errors and Debugging Skills"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.