簡體   English   中英

用分號替換分號

[英]Replace semicolon with a break in jquery

無論分號在哪里,我都想打破界限。

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

只需嘗試

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") ) 

我認為,這是秩序問題。 您需要首先分配要替換的字符串,然后替換並分配結果。

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

您的代碼將首先替換,然后分配要替換的字符串。 稍后,您重新分配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';

如果代碼是文本文字,則給定的代碼可以正常工作。 但是,如果要以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;

**

更新:

**

當您更新問題時,它也幫助我也更新了答案。您需要進行2處更改:

1。

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

2。

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

單擊此處進行演示

只需使用split()。 試試這個代碼

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;

希望這可以幫助

您可以使用。

 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