|
|
Subject:
"Regular Expressions in JavaScript"
Category: Computers > Programming Asked by: rc2000-ga List Price: $20.00 |
Posted:
05 Nov 2002 08:01 PST
Expires: 05 Dec 2002 08:01 PST Question ID: 99336 |
Is there anyone familiar with Regular Expressions in Java script. The issue: I have a string "#,#,#,#,#,#,#,#,#,#,#" the first three pound signs should equal visible and look like this "visible,visible,visible,#,#,#,#,#,#,#,#" The Last seven pound signs should equal to hidden so the final result would look like this "visible,visible,visible,hidden,hidden,hidden,hidden,hidden,hidden,hidden" Please help !!!! | |
| |
| |
| |
| |
| |
|
|
Subject:
Re: "Regular Expressions in JavaScript"
Answered By: tox-ga on 05 Nov 2002 14:39 PST Rated: |
There is really no need to use regular expressions to accomplish this task. According to your clarification, you are trying to hide the columns of a table that is outputted by a script am I correct? So, inside the <script> tags in your html document, paste the following function: BEGIN COPY AND PASTE ---------------------> function hideColumn(tableString, tableWidth, hideColumn) { newString = tableString; count = 1; while (newString.indexOf("#") > 0) { hidden = false; for (i=0;i<=hideColumn.length;i++) { if (hideColumn[i] == count) { hidden = true; } } if (hidden) { inString = "hidden"; } else { inString = "visible"; } newString = newString.substr(0,newString.indexOf("#")) + "\'visibility:" + inString + "\'" + newString.substr(newString.indexOf("#")+1); if (count == tableWidth) { count = 0; } count++; } return newString; } <--------------------------END COPY AND PASTE To use this function: tableString = hideColumn (tableString, widthOfTable, [columnsToHide[,columnsToHide]]) Assuming that the string you want to replace the # signs with is called 'myString', and the table that you are outputing is 5 columns by 3 rows, and that you wish to hide columns 2 and 5. Then do the following: myString = hideColumn (myString, 5, [2,5]); Make sure to include the square brackets around the columns you wish to hide. The string will become correctly formatted for whatever you may need it for (ex. document.write(myString);) |
rc2000-ga
rated this answer:
and gave an additional tip of:
$1.00
Not quite what I needed but it works for a temporary solution. The columns vary per ASP Page. With that in mind Regular Expressions would be used to create a RegX pattern as an object. Then I could use this for all ASP pages despite sql changes . With the short term solution I have to change the column numbers every time the sql select statement changes. Its code I did not have to write . RegX is definetly the ultimate solution !!! Again Thanks for the Short term solution .... |
|
Subject:
Re: "Regular Expressions in JavaScript"
From: tox-ga on 05 Nov 2002 20:08 PST |
Alright, an improved version of the script is: function hideColumn(tableString, hideColumn) { newString = tableString; count = 1; curLoc = 0; while (newString.indexOf("#") > 0) { hidden = false; columnStart = newString.indexOf("</tr>", curLoc); if (columnStart < newString.indexOf("#")) { curLoc = columnStart + 4; count=1; } for (i=0;i<=hideColumn.length;i++) { if (hideColumn[i] == count) { hidden = true; } } if (hidden) { inString = "hidden"; } else { inString = "visible"; } newString = newString.substr(0,newString.indexOf("#")) + "\'visibility:" + inString + "\'" + newString.substr(newString.indexOf("#")+1); count++; } return newString; } Now there's no need to specify the number of columns...only the columns that you wish to hide. |
Subject:
Re: "Regular Expressions in JavaScript"
From: hammer-ga on 06 Nov 2002 05:08 PST |
rc2000, There are lots of ways to skin this cat, but none of them are going to automatically adjust to changes in your SQL statement. The reason is that you are using the same character/pattern to indicate both hidden and visible. Therefore, your only indicator is column number, which can change with SQL changes. Any method of looping, indexing or counting # signs depends on knowing which indexes to change. If you have any control over the string you receive, you may want to attack this problem at an earlier point in your process. - Hammer |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |