Home  >  Article  >  Web Front-end  >  javascript how to remove empty lines in string

javascript how to remove empty lines in string

王林
王林Original
2021-10-27 16:47:194356browse

Javascript method to delete empty lines in a string: combine regular expressions to delete, such as [replace(/(\n[\s\t]*\r*\n)/g, '\ n').replace(/^[\n\r\n\t]*|[\n\r\n\t]*$/g, '');...].

javascript how to remove empty lines in string

#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.

We can use regular expressions to achieve this. Let’s take a look at the specific implementation code:

String.prototype.removeBlankLines = function () {
    return this.replace(/(\n[\s\t]*\r*\n)/g, '\n').replace(/^[\n\r\n\t]*|[\n\r\n\t]*$/g, '')
}

Running results:

javascript how to remove empty lines in string

Recommended Learn: javascript video tutorial

The above is the detailed content of javascript how to remove empty lines in string. 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