我怎么能这样看后面

时间:2016-12-16 13:30:09

标签: javascript regex

我有这样的文字:

var obj = 
{
    text: 'Contrary to popular belief, Lorem Ipsum is not simply random text.It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance.The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.'
}

我需要把new lines放在正确和点上。

我试试这个:

<script>
var obj = 
{
    text: 'Contrary to popular belief, Lorem Ipsum is not simply random text.It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance.The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.'
}

document.write(JSON.stringify(obj).split(/[^\sA-Z]\.(?=\s|[A-Z])/).join("."+"<br><br>"));

</script>

如何在不删除最后一个字母的情况下添加此新行?

1 个答案:

答案 0 :(得分:2)

您实际上可以在简单的replace方法中使用您的模式,只需使用$&<br><br>替换模式:

your_string.replace(/[^\sA-Z]\.(?=\s|[A-Z])/g, '$&<br><br>')

请参阅regex demo

$&反向引用是指整个匹配值,[^\sA-Z]\.子模式消耗的内容(除了空白和大写ASCII字母和点之外的任何字符) 。