簡體   English   中英

如何使用正則表達式在逗號后換行

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

我需要使用正則表達式在逗號后換行(也歡迎使用另一種方法)

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

您可以使用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())

可以使用正則表達式並替換“。” 休息一下。

這是可重復使用的 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