Home > Article > Web Front-end > How to regularly replace non-Chinese characters in javascript
In JavaScript, you can use the replace() function with the regular expression "/[u4e00-u9fa5|,] /ig" to find all non-Chinese characters in the string and replace them with other specified ones. Value, syntax "String object.replace(/[u4e00-u9fa5|,] /ig,'Specify replacement value')".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, you can use the replace() function with regular expressions to replace non-Chinese characters.
Thereplace() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.
string.replace(searchvalue,newvalue)
Parameter | Description |
---|---|
searchvalue | Required . A RegExp object that specifies the substring or pattern to be replaced. Note that if the value is a string, it is retrieved as a literal literal pattern rather than first being converted to a RegExp object. |
newvalue | Required. A string value. Specifies functions for replacing text or generating replacement text. |
Return value: a new string obtained by replacing the first match or all matches of regexp with replacement.
Example: Use the replace() function to replace non-Chinese characters
The regular expression used is:
/[u4e00-u9fa5| ,] /ig
Implementation code:
var str="a你b好7dfhg呀89"; console.log("原字符串: "+str); var value=str.replace(/[u4e00-u9fa5|,]+/ig,' '); console.log("正则替换后: "+value);
Extended knowledge: regular replacement of Chinese characters
The regular expression used is:
/[^u4e00-u9fa5|,] /
Implementation code:
var str="a你b好7dfhg呀89"; console.log("原字符串: "+str); var value=str.replace(/[^u4e00-u9fa5|,]+/ig,' '); console.log("正则替换后: "+value);
[Related recommendations: javascript video tutorial, programming video】
The above is the detailed content of How to regularly replace non-Chinese characters in javascript. For more information, please follow other related articles on the PHP Chinese website!