javascript - 如何防止段落中的行被删除(Javascript替换正则表达式)

标签 javascript regex

请看一下这个(regex101) regex link

var t = $('#text').text();
$('#cvText').html(t.replace(/\b([A-z']+)\b/ig, '<a href="#">$1</a>')).show();

这是jsfiddle link

我用它来捕获所有单词以及撇号。 但这也删除了我想保留的段落中的新行。

我该怎么做?

谢谢!

最佳答案

您可以使用以下代码:

$('#btn').on('click',function(e){
	var t = $('#text').text();
    console.log(t);
	$('#cvText').html(nl2br(t.replace(/\b([A-Z']+)\b/ig, '<a href="#">$1</a>'))).show();
});

function nl2br (str) {   
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br />$2');
}
div{ padding:10px; margin:10px; font-family:Arial, Helvetica, sans-serif; font-size:16px; line-height:30px; overflow-y:scroll }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id='text' style="width:500px; height:350px;">
The young woman, is known as the bride and she usually has two or more bridesmaids to hold her train. Her partner is called the groom and he is accompanied by the best man, who is usually his best friend. His main tasks are to get his friend to the ceremony sober and on time; to look after the ring(s); and to give a humorous speech later on, which is often designed to gently embarrass his friend.

Traditionally the principal men wear red carnations in their buttonholes, while the rest wear white ones. At the beginning of the church ceremony it is customary for the young woman's father to walk her down the aisle, to the accompaniment of organ music. The central, most important part of the ceremony is when the couple exchange their wedding vows.

At the end of the ceremony the guests leave while the couple and their witnesses sign the marriage register. When the couple come out of the ceremony, the guests often throw confetti (small pieces of coloured paper) at them. Then the couple together with their guests go to the reception. This can be held in a hotel, or sometimes in a marquee (a large tent) in a large garden. In the evening the young couple disappear to get changed into their going away clothes, before setting off for their honeymoon.
</textarea>
<br /><br /> 
<input type="button" id="btn" value="Convert" />

<div id="cvText" style="border:1px solid #000; width:500px; hei1ght:350px; display:none;"></div>

nl2br函数将插入<br /><br>在所有换行符之前( \r\n\n\r\n\r )。

该函数的核心是.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1<br />$2'); 。正则表达式将一个非 > 的可选符号匹配并捕获到组 1 中。或\r ,或\n 。然后,它匹配 1 个单换行符并将其捕获到组 2 中。替换由第一个捕获组(<br />)组成。和第二个捕获的文本(换行符)。

请参阅regex in action at regex101.com .

关于javascript - 如何防止段落中的行被删除(Javascript替换正则表达式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33630626/

相关文章:

javascript - 组合对象数组

javascript - 删除 jsp foreach 中的内联 javascript

ruby - 在 Ruby 中将字符串的部分与数组的元素匹配

java.lang.StringIndexOutOfBoundsException : from java. util.regex.Matcher

c# - 如何替换正则表达式中匹配的一部分

Java - 标记化参数列表

javascript - 在 javascript 中使用混洗算法跟踪索引

javascript - 为什么在我的 AJAX 调用中,response.chartAt(0) 返回未定义?

javascript - 我的 redux 助手应该如何引用调度?

java - 正则表达式中的性能问题