i have string and i need to replace tabs (white space) with "|"..the function work fine but the problem is there is more than one tab together so it show like this ( this||is|||text|||| ) and what i need is ( this|is|text ) here is example.
$(document).ready(function() {
var text = $(".para").text();
$(".result").text(text.replace(/\t/g, "|"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="container">
<p class="para">
Aa aa Aaa aa
Bb bb Bb bb Bbb bb bb
cc cccc C ccccc
dd Dd d
Ee e
fff fff
</p>
<p class="result"></p>
</div
答案 0 :(得分:4)
Use a regex with the g
flag:
myString.trim().replace(/\t+/g, '|')
The +
indicator groups all the \t
together. Also do a .trim()
to remove the spaces at beginning/end