用jquery中断替换分号

时间:2016-07-08 09:17:41

标签: javascript jquery replace split

我希望在有分号的地方打破界限。



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




5 个答案:

答案 0 :(得分: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") )




答案 1 :(得分: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';

答案 2 :(得分:1)

如果代码是文本文字,代码将正常工作。但是,如果您想以HTML格式显示,则需要使用 <br/> \ 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;

**

  

更新:

**

当您更新问题时,它帮助我更新了答案.. 您需要进行2项更改:

1

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

2

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

CLICK HERE FOR FIDDLE DEMO

答案 3 :(得分:0)

只需使用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;

希望这有帮助

答案 4 :(得分:0)

你可以使用。

&#13;
&#13;
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);
&#13;
&#13;
&#13;