使用jQuery查找并加入两行 [英] Find and join two lines using jquery

查看:88
本文介绍了使用jQuery查找并加入两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当冒号在使用正则表达式的第一行中匹配时,我想加入下一行.我尝试了所有正则表达式的替换,但是没有运气.有人可以帮助我吗,这是我尝试过的代码.

I want to join next line when Colon is matches in first line using regex I tried all regex replace but no luck Can anyone help me please here is code what I tried. Pr

function copy() {
  var txt = $('#result1').val().replace(/prefix_ago/g, ' : <br />');
  txt = txt.replace(/(?:\r\n|\r|\n)/g, '');

  $('#id1').html(txt.trim());
}

我的对话中有一个唯一的字词是prefix_ago,我将其替换为冒号和

there is unique word in my conversation that is prefix_ago which I am replacing to Colon and

<textarea class="result" id="result1" rows="10" cols="150" style="font-size:11px;resize: none; width:225px;">
User 1 prefix_ago 
Hi How are you 
User 2 prefix_ago
I am good How about you 
 </textarea> 
<button onClick="copy()" > Copy & Replace</button>
<div id="id1">

</div>

推荐答案

function copy() {

var regex = /^(.*?prefix_ago)/gm;
var subst = '\#$1';
var originaltxt = $('#result1').val().replace(regex, subst);
originaltxt = originaltxt.replace(/(#)/gm, '<br>');
$('#id1').html(originaltxt.trim());
}




<textarea class="result" id="result1" rows="10" cols="150">
        User 1 prefix_ago 
        Hi How are you 
        User 2 prefix_ago
        I am good How about you 
         </textarea> 

        <button onClick="copy()" > Copy & Replace</button>
        <div id="id1">

</div>

当正则表达式匹配prefix_ago时,我插入了#标签,这将使所有内容变成单行,然后我使用相同的#标签插入了换行符,然后删除了#标签.

I have inserted # tag when regex matches prefix_ago, this is will bring all into single lines and then I have break lines using same # tag and then I removed # tag.

这篇关于使用jQuery查找并加入两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆