Regular method of replacing newline characters: 1. Use the replace() function, the syntax is "String object.replace(/[\r\n]/g,'replacement value')"; 2. Use replaceAll () function, syntax "string object.replaceAll(/[\r\n]/g,'replacement value')".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript regular replacement of newlines
Method 1: Use the replace() function
replace The () method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.
Example:
let a = '\n换行个阿斯蒂芬\r换行个阿斯蒂芬ABCD'; console.log(a); let b = a.replace(/[\r\n]/g,''); console.log(b);
Method 2: Use replaceAll() function
replaceAll() method is used Replace some characters with other characters in a string, or replace a substring that matches a regular expression. This function will replace all matching substrings.
let a = '换行个阿斯蒂芬\r\n换行个阿斯蒂芬\nABCD'; console.log(a); let b = a.replaceAll(/[\r\n]/g,'!'); console.log(b);
[Related recommendations:javascript video tutorial,web front-end】
The above is the detailed content of How to use regular expression to replace newline characters in javascript. For more information, please follow other related articles on the PHP Chinese website!