2016-07-08 33 views
0

我想在分号存在的地方打破分号。在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);

+0

它看起来确定你能指望什么 – guradio

回答

3

只是一味

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

+0

是不是意味着要拆分'( “;”)'? – gcampbell

+0

@gcampbell是的,匆忙犯了一个错字 – gurvinder372

0

只需使用分裂()。试试这个代码

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; 

希望这有助于

2

我认为,它的顺序问题。您需要首先分配替换字符串,然后替换并分配结果。

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'; 
0

您可以使用。

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

+0

分隔符是';'不是''' –

+1

我认为你应该用'back slash' =>'/ \;/g'来转义';'。 –

+0

谢谢@mortezaT。 –

1

定的代码将工作的情况下,罚款是文字文本。但是,如果你想在HTML显示它您需要使用insteed的\ 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; 

**

修订<br/>

**

当你更新你的问题时,它帮助我更新了答案。 你需要做2点的变化:

1.

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

2.

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

CLICK HERE FOR FIDDLE DEMO

+0

时尝试。但不起作用 –

+0

请添加一些html。你如何显示? – Tuhin