简体   繁体   English

用分号替换分号

[英]Replace semicolon with a break in jquery

I want to break the line wherever semicolon is there. 无论分号在哪里,我都想打破界限。

 var locdata = { "locations": [{ "id": 0, "name": 'USA', "cx": 100, "cy": 100, "address":'545, 8th ABCDddd, Suite 17SW;New City, NY 10fg018;Tel: 1-21g2-24448-2727/Fax: 1-552-268-7825;Toll Free: 1-866-383-8806;Mr. Johny Pleeto;DirectManager;Mobile no. 1-917-605-0022'; var address = locdata.locations[idvar].address; var result = address.replace(/\\;/g,'\\n'); address = result; $('div#address').text(address); 

Simply try 只需尝试

split(";").join("\n")

 var address = '545, 8th ABCDddd, Suite 17SW;New City, NY 10fg018;Tel: 1-21g2-24448-2727/Fax: 1-552-268-7825;Toll Free: 1-866-383-8806;Mr. Johny Pleeto;DirectManager;Mobile no. 1-917-605-0022'; console.log( address.split(";").join("\\n") ) 

I think, its a problem of order. 我认为,这是秩序问题。 You need first to assign the string for replacing, then replace and assign the result. 您需要首先分配要替换的字符串,然后替换并分配结果。

 var address = '545, 8th ABCDddd, Suite 17SW;New City, NY 10fg018;Tel: 1-21g2-24448-2727/Fax: 1-552-268-7825;Toll Free: 1-866-383-8806;Mr. Johny Pleeto;DirectManager;Mobile no. 1-917-605-0022', result = address.replace(/\\;/g,'\\n'); console.log(result); 

Your code replaces first and then assign the string for replacing. 您的代码将首先替换,然后分配要替换的字符串。 Later you reassign address and then you assign a string. 稍后,您重新分配address ,然后分配一个字符串。

var result = address.replace(/\;/g,'\n');  
    address = result;
var address = '545, 8th ABCDddd, Suite 17SW;New City, NY 10fg018;Tel: 1-21g2-24448-2727/Fax: 1-552-268-7825;Toll Free: 1-866-383-8806;Mr. Johny Pleeto;DirectManager;Mobile no. 1-917-605-0022';

Given code will work fine in case it is text literal. 如果代码是文本文字,则给定的代码可以正常工作。 But If you want to display it in HTML you need to use <br/> insteed of \\n 但是,如果要以HTML格式显示它,则需要使用\\ n的 <br/>指示

<div id="mydiv"></div>

var address = '545, 8th ABCDddd, Suite 17SW;New City, NY 10fg018;Tel: 1-21g2-24448-2727/Fax: 1-552-268-7825;Toll Free: 1-866-383-8806;Mr. Johny Pleeto;DirectManager;Mobile no. 1-917-605-0022';
var result = address.replace(/\;/g,'<br/>');  
document.getElementById("mydiv").innerHTML=result;

** **

UPDATED: 更新:

** **

As you updated your question, It helped me to update the answer too.. You need to do 2 changes: 当您更新问题时,它也帮助我也更新了答案。您需要进行2处更改:

1. 1。

address.replace(/\;/g,'<br/>');  

2. 2。

$('div#address').html(address);

CLICK HERE FOR FIDDLE DEMO 单击此处进行演示

Just use split(). 只需使用split()。 Try this code 试试这个代码

var address = '545, 8th ABCDddd, Suite 17SW;New City, NY 10fg018;Tel: 1-21g2-24448-2727/Fax: 1-552-268-7825;Toll Free: 1-866-383-8806;Mr. Johny Pleeto;DirectManager;Mobile no. 1-917-605-0022';
var result = address.split(";");
address = result;

Hope this helps 希望这可以帮助

You can use. 您可以使用。

 var address = '545, 8th ABCDddd, Suite 17SW;New City, NY 10fg018;Tel: 1-21g2-24448-2727/Fax: 1-552-268-7825;Toll Free: 1-866-383-8806;Mr. Johny Pleeto;DirectManager;Mobile no. 1-917-605-0022'; var result= address.replace(/\\;/g, '\\n'); alert(result); 

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

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