Jump to content
  • Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~
Greywacke

Splitting A String By "<br />"

Recommended Posts

hi,its me again with one of my weakpoints...regular expressions this time, i need to split a string of attributes: as a list of keys = variables, by splitting a string by "<br />".next the script will try to seperate the keys and values by " = ".the problem with this is that i cannot seem to split by html tags, as i have tried to do below.

arrt = arr[3].split(/<.*?>/g)[0];alert(arrt.join("\n"));var attr = new Array();for (var i = 0; i < arrt.length; i++) {	alert(arrt[i].split(" = "));	attr[i] = arrt[i].split(" = ");}

arr[3] is the attributes list retrieved from XML, the variable formatting differs accross sources such as the follows:source A

canopy_req = pre-owned_white<br/>canopy_style = low_or_highline_standard<br/>budget = R5,000 to R7,500<br/>fitment = within_month<br/>vehicle_status = possession_yes<br/>vehicle_make_model = Isuzu - DCAB<br/>year_model = 2006
source B
products_description = Nissan LWB Canopy - Half Door<br />Bakkie model = 2002 - current model<br />Requirement = Pre-owned - White<br />Colour code = White Non-coded (standard)<br />Budget = R3,000 to R4,000<br />Fitment = Within the next 2 weeks
this needs to be done in order to parse the attributes for required attributes (the key in this instance will contain "req" of various casing depending on the source of the lead.as i am not that confident with regular expressions, i will need to ask for help.there are a few ways to skin this cat, i could search for the attributes in the string, but then that would be hardcoding and apply only to these two sources. an array with subarrays allows me to check the variables for case-insensitive "req" to find the required attribute.any help would be appreciated - even just a nudge in the right direction :) Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

Why not just this:arrt = arr[3].split("<br />")[0];orarrt = arr[3].split(/<br \/>/g)[0];Or to do what I think you were trying to do (split on any HTML tag):arrt = arr[3].split(/<.+>/g)[0];

Share this post


Link to post
Share on other sites

thanks jkloth,yes - i was trying to split on any HTML tag, due to the differences in the delimiters... :)gonna try your version now :)=======================okay, tried and a few changes have occured to the code.

var opt1 = document.createElement('option');arrt = arr[3].split(/<.+>/g);alert(arrt.join("\n"));var attr = new Array();for (var i = 0; i < arrt.length; i++) {	alert(arrt[i].split(" = "));	attr[i] = arrt[i].split(" = ")[0];}

thankyou for helping out with that regular expression :)

Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

hmmm... seems i have found a temporary solution to my problem, but this arises another error that i can't figure out for the life of me...(oh the woes of working under a galvanised iron roof without aircon!)here is the updated piece of code:

//alert(arr[3]);var opt1 = document.createElement('option');arrt = arr[3].split(/<.+>/g);//alert(arrt);var attr = new Array();for (var i = 0; i < arrt.length; i++) {	attr[i] = arrt[i].split(" = ")[0];}var prod = "";var cstl = "";var preq = "";for (var i = 0; i < attr.length; i++) {	if (attr[i][0].toLowerCase().indexOf("req") != -1} {		preq = attr[i][1];	} else if (attr[i][0].toLowerCase().indexOf("prod") != -1 || attr[i][0].toLowerCase().indexOf("make") != -1) {		prod = attr[i][1];	} else if (attr[i][0].toLowerCase().indexOf("make") != -1) {		cstl = attr[i][1];	}}opt1.text = "\u00a0\u00a0\u00a0#" + (parseInt(arr[0]) + 9001100) + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0" +		arr[6] + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0" +		prod + cstl + " ( " + preq + " )";

unfortunately, this generates the following error in firefox...

Error: missing ) after conditionSource File: http://www.ferrety.co.za/fab/scripts/ajax_prospects.jsLine: 607, Column: 53Source Code: if (attr[0].toLowerCase().indexOf("req") != -1} {
please help! i am battling to see the forest for the trees...oh please do not click the link, i think it requires a password. the relevant code is above. Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

oh nm, theres a curly closing brace instead of a normal closing brace...the problem now is that this doesn't retrieve the vehicle_make_model & canopy_style, canopy_req or products_description, Requirement attributes... 0o what could possibly be wrong here? 0o

Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

What do you mean, the preq, prod, and cstl variables are empty? You can use console.log to send the whole attr array to Firebug so that you can see the data there, it might not be what you expect.

Share this post


Link to post
Share on other sites

the array preperation code has been changed now, i discovered i was only populating the attr array with element 0 from the split (*doh* :)).this has almost been completed (kind of been resolved), here is the updated code:

alert(arr[3]);var opt1 = document.createElement('option');arrt = arr[3].split(/<.+>/g);//alert(arrt);var attr = new Array();for (var i = 0; i < arrt.length; i++) {	attr[i] = arrt[i].split(" = ");	alert(attr[i].join("\n"));}var produ = "";	// product namevar pstyl = "";	// product stylevar prequ = "";	// product requirementfor (var n = 0; n < attr.length; n++) {	alert(attr[n][0].toLowerCase()+"\n"+attr[n][0].toLowerCase().indexOf("req")+	"\n"+attr[n][0].toLowerCase().indexOf("prod")+	"\n"+attr[n][0].toLowerCase().indexOf("make")+	"\n"+attr[n][0].toLowerCase().indexOf("styl"));	if (attr[n][0].toLowerCase().indexOf("req") != -1) {		prequ = attr[n][1];	} else if (attr[n][0].toLowerCase().indexOf("prod") != -1 ||		   attr[n][0].toLowerCase().indexOf("make") != -1) {		produ = attr[n][1];	} else if (attr[n][0].toLowerCase().indexOf("styl") != -1) {		pstyl = attr[n][1];	}}opt1.text = "\u00a0\u00a0\u00a0#" + (parseInt(arr[0]) + 9001100) + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0" +arr[6] + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0" +produ.concat(pstyl) + " ( " + prequ + " )";

ps justsomeguy, if i alert the data that its processing - that surely shows whats there? (especially when regarding strings) for some reason its not parsing all the attributes though... for some reason, it's only parsing the first two or so elements of the array which theoretically includes two subelements, the attribute key and value.

Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

the problem is with the split(/<.+>/g) though... before that i have all the attributes, it only returns the first two which are not always the attributes wanted.well i did some checking, and /<.+>/g does equal any html tag - the . = any character, the plus means any occurrence count of the preceding character.perhaps the / in the br's (xhtml line break as opposed to <br>) are breaking the regular expression, i don't know... it could even be the >.

Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

Alert only shows scalar data, if you send an array to alert it just shows "Array". But if you send it to Firebug's console you'll actually be able to look at all of the values in it. It's much easier to debug when you can track the actual data instead of making assumptions about it though. You could write a loop to go through the array and alert everything, but then you see every element only once and you have to click through all of the boxes, it's much easier to inspect the array directly in the DOM through the console.

Share this post


Link to post
Share on other sites

in firefox it shows the array elements, joined with a , though. the current site is an admin site for use in firefox. i can make out scalar, because i know what the data type is of the variable alerted. :)i did click the alerts one at a time, stepping through the elements in the array via the script. i've installed firebug now as you suggested, but i don't see how this changes what i already know beyond a doubt...

Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

So what's the before and after? What's the string you're splitting, and what array does that result in?

Share this post


Link to post
Share on other sites
products_description = Tata D/C Canopy - Half Door<br />Bakkie model = 2004 - current model<br />Requirement = New - White<br />Colour code = White Non-coded (standard)<br />Budget = R3,000 to R4,000<br />Fitment = ASAP
is the string that is split first.this split value, equals the following two arrays (first and last attributes in the string), elements on a new line. it is looping through the entire split.
products_descriptionTata D/C Canopy - Half Door
FitmentASAP
then it loops through the 'saved arrays", validating them to find if it is a needed attribute.
products_description-10-1-1
and this is the second element.
fitment-1-1-1-1
what happened to the other elements? 0o Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

this is the current script:

// ONLY PARSING THE FIRST TWO ATTRIBUTES... WTF?alert(arr[3]);var opt1 = document.createElement('option');arrt = arr[3].split(/<.+\/>/g,-1);//alert(arrt);var attr = new Array();for (var i = 0; i < arrt.length; i++) {	attr[i] = arrt[i].split(/.=./g,-1);	alert(attr[i].join("\n"));}var produ = "";	// product namevar pstyl = "";	// product stylevar prequ = "";	// product requirementfor (var n = 0; n < attr.length; n++) {	alert(attr[n][0].toLowerCase()+"\n"+attr[n][0].toLowerCase().indexOf("req")+	"\n"+attr[n][0].toLowerCase().indexOf("prod")+	"\n"+attr[n][0].toLowerCase().indexOf("make")+	"\n"+attr[n][0].toLowerCase().indexOf("styl"));	if (attr[n][0].toLowerCase().indexOf("req") != -1) {		prequ = attr[n][1];	} else if (attr[n][0].toLowerCase().indexOf("prod") != -1 ||	attr[n][0].toLowerCase().indexOf("make") != -1) {		produ = attr[n][1];	} else if (attr[n][0].toLowerCase().indexOf("styl") != -1) {		pstyl = attr[n][1];	}}opt1.text = "\u00a0\u00a0\u00a0#" + (parseInt(arr[0]) + 9001100) + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0" +arr[6] + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0" +produ.concat(pstyl) + " ( " + prequ + " )";// ONLY PARSING THE FIRST TWO ATTRIBUTES... WTF?

how would i be able to select a possible whitespace, such as a space, nobreak space, tab or carriage return or even a new line or none at all in two positions. these are indicated by the X's in the following regexp:/X<.+X\/>X/g

Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

This is a "greedy" match. This pattern:/<.+>/gis matching this:products_description = Tata D/C Canopy - Half Door<br />Bakkie model = 2004 - current model<br />Requirement = New - White<br />Colour code = White Non-coded (standard)<br />Budget = R3,000 to R4,000<br />Fitment = ASAPGreedy means that it matches as much as it possibly can, ungreedy means it matches as little as it possibly can. You can actually just modify your pattern so that it looks for "<", then any character that is not ">", then ">"./<[^>]+>/gI think that should work, I'm pretty sure that's telling it to find "<", then one or more characters that are not ">", followed by ">".

Share this post


Link to post
Share on other sites

thanks justsomeguy :) i'll try this right away :)AWESOME :) as i said earlier there where many ways to skin a cat :) this seems the most straight forward however ;)

Edited by Pierre 'Greywacke' du Toit

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×