简体   繁体   English

替换所有字符串jQuery中的选项卡

[英]replace tabs in all string jquery

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. 我有字符串,我需要用“ |”替换制表符(空白)。该功能可以正常工作,但问题是在一起的制表符不止一个,因此显示如下(此||是|||文本|| ||),而我需要的是(this | is | text)示例。

 $(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 

Use a regex with the g flag: 将正则表达式与g标志一起使用:

myString.trim().replace(/\t+/g, '|')

The + indicator groups all the \\t together. +指示器将所有\\t组合在一起。 Also do a .trim() to remove the spaces at beginning/end 还要执行.trim()来删除开头/结尾的空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM