首页 > 解决方案 > 如何使用正则表达式在逗号后换行

问题描述

我需要使用正则表达式在逗号后换行(也欢迎使用另一种方法)

function breakLineAfterComma(){
    let Text = "This is an Example. Break Line Here"
    Text.replace('regex here')
    return Text;
}

标签: javascript

解决方案


您可以使用String.replace换行符替换所有句点和空格:

function breakLineAfterComma(){
    let Text = "This is an Example. Break Line Here. Break Line Here"
    return Text.replace(/\. /g, "\n");
}

console.log(breakLineAfterComma())

您还可以使用String.replaceAll

function breakLineAfterComma(){
    let Text = "This is an Example. Break Line Here. Break Line Here"
    return Text.replaceAll(". ", "\n");
}

console.log(breakLineAfterComma())


推荐阅读