Google Answers Logo
View Question
 
Q: "Regular Expressions in JavaScript" ( Answered 2 out of 5 stars,   2 Comments )
Question  
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 !!!!

Request for Question Clarification by seizer-ga on 05 Nov 2002 08:11 PST
Regular expressions are probably not the best way to solve your
problem. Could you elaborate on why you can't just write the string
the way you want it at the start?

That is, why do you start with the string of pound signs, when you
could just write visible, visible, visible, hidden, etc

Thanks!

Request for Question Clarification by studboy-ga on 05 Nov 2002 08:35 PST
Hi,

Is this what you are looking for?  If yes, let me know so I can 
post a formal answer:

<SCRIPT LANGUAGE="JavaScript1.2">
	teststr="this is a test #,#,#,#,#,#,#";
	re = /#,#,#,#,#,#,#/;
	newre="visible,visible,visible,hidden,hidden,hidden,hidden";
	newstr = teststr.replace(re, newre);
	document.write(newstr)
</SCRIPT

Request for Question Clarification by studboy-ga on 05 Nov 2002 11:24 PST
Hi,

Wouldn't it be OK to look for a "longer" matching string?
Instead of looking for /#/ you can look for /<tr><td style =# >column1row1<\/td>/,
etc., and replace accordingly, no?

Thanks

Request for Question Clarification by hammer-ga on 05 Nov 2002 11:31 PST
Are there always only two rows per string, or are there a variable
number of rows per string?

Clarification of Question by rc2000-ga on 05 Nov 2002 13:13 PST
Clarification for :studboy-ga
         I cannot include the column data it is dynamic however style
= # is
constatnt . However I could use <td style = and use the </tr as my row
identifier for a new row in the pattern sequence.




Clarification for :hammer-ga 
         There could be unlimited rows however, 10 rows is probably
the max

Request for Question Clarification by studboy-ga on 05 Nov 2002 14:09 PST
Hi,

Have you tried the "memorize and replace" aspect of regular
expression?

<SCRIPT LANGUAGE="JavaScript1.2">  
 teststr="<table> <tr><td style =#>column1row1</td><td style
=#>column2row1</td><td style=# >column3row1</td></tr><tr><td style
=#>column1row2</td><td style =# >column2row2</td><td
style=#>column3row2</td></tr></table>"
 re = /(.+)#(.+)#(.+)#(.+)#(.+)#(.+)#(.+)/;
 newre="$1 visible $2 visible $3 hidden $4 visible $5 visible $6
hidden $7";
 newstr = teststr.replace(re, newre);  
 document.write(newstr) 
</SCRIPT>

Let me know if this works for you.  Thanks.
Answer  
Subject: Re: "Regular Expressions in JavaScript"
Answered By: tox-ga on 05 Nov 2002 14:39 PST
Rated:2 out of 5 stars
 
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:2 out of 5 stars 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 ....

Comments  
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

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy