Home > Web Front-end > Front-end Q&A > How to regularly replace non-Chinese characters in javascript

How to regularly replace non-Chinese characters in javascript

青灯夜游
Release: 2022-10-13 17:37:12
Original
2048 people have browsed it

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')".

How to regularly replace non-Chinese characters in javascript

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)
Copy after login
ParameterDescription
searchvalueRequired . 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.
newvalueRequired. 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);
Copy after login

How to regularly replace non-Chinese characters in javascript

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);
Copy after login

How to regularly replace non-Chinese characters in javascript

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template