[英]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.