简体   繁体   中英

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.

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

<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:

1.

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

2.

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

CLICK HERE FOR FIDDLE DEMO

Just use 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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