开发者

extract out text in each line and put it in seperate variables with javascript

开发者 https://www.devze.com 2023-02-10 01:35 出处:网络
I have the following address which is in a paragraph with no great way to select the individual text areas within. I do not have access 开发者_JAVA技巧to this code. I want to extract out each line and

I have the following address which is in a paragraph with no great way to select the individual text areas within. I do not have access 开发者_JAVA技巧to this code. I want to extract out each line and put the text values in a variable for each type. Not sure what would be the best way to do it.

Use .split('<br>') ? then use .split('&nbsp;') to separate the state from the zip same with city and state I am a little lost here.

Here are the variables I would like.

company name, person's name, address1, city, state, country, zip code, phone,

Here is the paragraph that i have. I do not need the '(Residential Address)'

<p>
XYZ Inc<br>
John&nbsp;Smith<br>
555 Anywhere Street<br>
New York, NY&nbsp;11150<br>
United States<br>
212-555-1212<br>

 (Residential Address)


</p>


Given a string variable str containing your data:

var str=str.split("<br>"),
companyName=str[0],
personName=str[1],
address1=str[2],
location=str[3].split(","),
city=location[0],
location=location[1].split(" "),
state=location[0],
zip=location[1],
country=str[4],
phone=str[5];

If you give your <p> an id: <p id="values">, you can do this:

var str=document.getElementById("values").innerHTML;


This is what actually worked with the help of your code JC. I needed to change the second split to split against &nbsp instead of " " and I trimmed all the variables even though some don't really need to.

var shipping_info=$('p').html();
var str=shipping_info.split('<br>');
var companyName=jQuery.trim(str[0]);
var personName=jQuery.trim(str[1]).replace('&nbsp;',' ');
var address1=jQuery.trim(str[2]);
var location1=str[3].split(',');
var city=jQuery.trim(location1[0]);
var location1=location1[1].split('&nbsp;');
var state=jQuery.trim(location1[0]);
var zip=jQuery.trim(location1[1]);
var country=jQuery.trim(str[4]);
var phone=jQuery.trim(str[5]);
alert(companyName);
alert(personName);
alert(address1);
alert(city);
alert(state);
alert(zip);
alert(country);
alert(phone);
0

精彩评论

暂无评论...
验证码 换一张
取 消