Home  >  Article  >  Web Front-end  >  How to remove the carriage return character in javascript

How to remove the carriage return character in javascript

青灯夜游
青灯夜游Original
2021-04-19 17:54:586457browse

In JavaScript, you can use the replace() function with the regular expression "/[\r\n]/g" to remove the carriage return. The syntax format is "Str.replace(/[\r\n]/g" , "")". The replace() method is used to replace the substring matching the regular expression with some characters in the string.

How to remove the carriage return character in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Removing carriage returns in javascript

In javascript, you can use the replace() method with regular expressions to remove spaces, carriage returns, and line feeds, which is very efficient.

Thereplace() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.

Example:

function iGetInnerText(testStr) {
        var resultStr = testStr.replace(/\ +/g, ""); //去掉空格
        resultStr = testStr.replace(/[ ]/g, "");    //去掉空格
        resultStr = testStr.replace(/[\r\n]/g, ""); //去掉回车换行
        return resultStr;
    }
    
var testStr="sssss 

vvvvv";
var result=iGetInnerText(testStr);

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to remove the carriage return character in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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