插入第一个逗号&amp;用<br/>替换所有点

时间:2016-12-11 07:02:25

标签: javascript jquery

如何为第一个逗号和每个点(句点)插入<br>

&#13;
&#13;
<script>
var test = $('.test').text();
var result = test.replace(/\./g,'.<br/>');   
$('.test').html(result);
</script>
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">Hi,Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto, optio, dolorum provident rerum aut hic quasi placeat iure tempora laudantium ipsa ad debitis unde? Iste voluptatibus minus veritatis qui ut.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto, optio, dolorum provident rerum aut hic quasi placeat iure tempora laudantium ipsa ad debitis unde? Iste voluptatibus minus veritatis qui ut.</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

您可以再次致电replace

var result = test.replace(/\./g,'.<br/>').replace(',', '<br/>');  

答案 1 :(得分:0)

如果您需要替换第一个逗号,可以使用以下内容:

<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>


<div class="row">
  <div class="col-lg-12">

    <div class="col-lg-3">

      <div class="form-group">
        <input type="text" class="form-control" placeholder="specify the date of the loan agreement" name="pickyDate" id="pickyDate" />
      </div>

      <div class="form-group">
        <input type="text" class="form-control" placeholder="name of the day" name="daypk" id="daypk" disabled/>
      </div>

      <div class="form-group">
        <input type="text" class="form-control" placeholder="dateinword" name="dateinword" id="dateinword" disabled/>
      </div>

      <div class="form-group">
        <input type="text" class="form-control" placeholder="will be dd/mm/yy in word" name="worddate" id="worddate" disabled/>
      </div>


    </div>
  </div>
</div>

演示:https://jsfiddle.net/4jgyj98c/

答案 2 :(得分:-1)

您需要在加载jquery后调用jquery部分,并使用

$(document).ready(function(){
  var test = $('.test').text();
  var result = test.replace(/\./g,'.<br/>');
  // var result = test.split('.').join('.<br/>');
  $('.test').html(result);
})

将是一个很好的prectice。所以把你所有的代码都放到这个函数中。 我还提到了另一种方法。你也可以尝试一下。两者都会这样做。