简体   繁体   English

如何使用正则表达式在逗号后换行

[英]How to use Regex to break line after comma

I need to break line after comma using regex(another method is welcome too)我需要使用正则表达式在逗号后换行(也欢迎使用另一种方法)

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

You can use String.replace to replace all periods and a space with a newline:您可以使用String.replace将所有句点和空格替换为换行符:

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

You can also use String.replaceAll :您还可以使用String.replaceAll

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

it's possible to use regex and replace "."可以使用正则表达式并替换“。” with a break.休息一下。

here is the reusable function example这是可重复使用的 function 示例

function breakLineAfterComma(text){
    return text.replace(/\./g, '\n');
}

const content = "This is an Example. Break Line Here"
const a = breakLineAfterComma(content);
console.log(a);
function splitText(text){
  text = "This is an Example. Break Line Here";
  return text.split('. ').join('.\n');
// This is an Example. 
// Break Line Here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM