...

View Full Version : Javascript match for regular expression



xaverian
06-26-2012, 06:17 PM
Hi,

How can I match a hyphen/dash("-") in javascript by using regular expression.

I am using the following statement to match a single dash ("-") in one of my table cells as :

if (mytab.rows[i].innerHTML.match("-") ) {

// some statements

}

But it is matching both with a single dash ("-") and a dash within any other string e.g. "2005r02-iw". Please help me with the correct expression which will only match a single dash ("-") and not when it is within/before/after other characters .

Thanks in advance !!

Philip M
06-26-2012, 06:50 PM
You want to match a single hyphen and nothing but.


var x = mytab.rows[i].innerHTML;
if (/^-$/g.test(x)) {
alert ("Single hyphen matched");
}


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

xaverian
06-26-2012, 07:15 PM
Hi Philip,

Yes , I want to match only single hyphen ("-") and nothing else.

But the solution is not working. Actually my entire code is like this

<script type="text/javascript">
mytab=document.getElementById("maintab")
function showRows(val){
for (var i = 0; i < mytab.rows.length; i++) {
if (val=="all"){
mytab.rows[i].style.display="table-row"
} else {

if(mytab.rows[i].innerHTML.match("-") )){
mytab.rows[i].style.display="none"
}
} //else blk end
}
} ;

</script>

In the else block now I am doing it as :

else {
var x - mytab.rows[i].innerHTML;

if(/^-$/g.test(x)){
mytab.rows[i].style.display="none"
}

Please let me know if I am doing anything wrong.

Philip M
06-26-2012, 07:18 PM
mytab.rows[i].style.display="table-row"

That is not valid css.

A typo - var x - mytab.rows[i].innerHTML;
should be var x = mytab.rows[i].innerHTML;

xaverian
06-26-2012, 07:29 PM
Hi,
The css "table-row" you complain about has been working properly so far.

And I corrected that to var x = mytab.rows[i].innerHTML ;

But it is still not working.

Thanks.

Philip M
06-26-2012, 07:43 PM
Hi,
The css "table-row" you complain about has been working properly so far.

And I corrected that to var x = mytab.rows[i].innerHTML ;

But it is still not working.

Thanks.

Well do some debugging. Place some alerts at strategc postions. Examples:-

var x = mytab.rows[i].innerHTML;
alert (x);
if (/^-$/g.test(x)) {
alert ("Single hyphen matched");
}

function showRows(val){
alert (val);

xaverian
06-27-2012, 11:57 AM
Hi Philip,

Thanks , it helped. I put some alert to debug and a more stringent expression as :
if (/\W\-\W/.test(mytab.rows[i].innerHTML)){

But just one more issue, the thing is I am using this javascript in the following html wherein there is a "Status" column with drop-down. If I select "valid" then all the rows having some status (IN,CO) will appear and the rows having "-" as status will be discarded. It is working fine but please notice after filtering the content of the third and fourth rows are shifting from their corresponding column headers. I could not find a reason for that, would be great if you can point me out or provide a solution


<html>
<head>
<title>Collections</title>

<TABLE BORDER=1 id="maintab" ><TR BGCOLOR="#D0D0D0">
<TH ALIGN=LEFT>Task</TH><TH ALIGN=LEFT>Command</TH><TH ALIGN=LEFT>Method</TH><TH
ALIGN=LEFT>Status<BR>


<select onchange="showRows(this.value)">
<option value="all">show status</option>
<option value="all">all</option>
<option value="active">valid</option>
</select>


</TH><TH ALIGN=LEFT>Task version</TH><TH ALIGN=LEFT>Tool version</TH><TH
ALIGN=LEFT>Last changed</TH><TH ALIGN=LEFT>Comments</TH></TR>

<script type="text/javascript">
mytab=document.getElementById("maintab")
function showRows(val){

alert (val);
for (var i = 0; i < mytab.rows.length; i++) {
if (val=="all"){
mytab.rows[i].style.display="table-row"
} else {

var x = mytab.rows[i].innerHTML;
//alert (x);
if (/\W\-\W/.test(mytab.rows[i].innerHTML)){
//alert ("Single hyphen matched");

mytab.rows[i].style.display="none"
}
} //else blk end
}
} ;

</script>

<TR><TD rowspan=1><A HREF="">task_template</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:33:24</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_commands</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 16:39:14</TD><TD NOWRAP>forced by lintz</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_commands</A></TD><TD rowspan=1>func_verify</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=3><A HREF="method.pl?path=/home/lintz/">task_template_commands</A></TD><TD rowspan=3>methods_options</TD><TD NOWRAP>method1</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method2</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method3</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:48:51</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=3><A HREF="method.pl?path=/home/lintz/">task_template_commands</A></TD><TD rowspan=3>with_methods</TD><TD NOWRAP>method1</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method2</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method3</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:49:17</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_commands</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">with_options</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Thu Dec 9 2010 11:56:59</TD><TD NOWRAP>forced by lintz</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_exec_fail</A></TD><TD rowspan=1>create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_exec_partially_fail</A></TD><TD rowspan=1>create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_exec_partially_fail_coll_scope</A></TD><TD rowspan=1>create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=2><A HREF="method.pl?path=/home/lintz/">task_template_methods</A></TD><TD rowspan=2><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>method1</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:34:14</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method2</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:35:12</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_methods_exec_fail</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>method1</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:37:08</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_oodc</A></TD><TD rowspan=1>create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_oodc_no_inData</A></TD><TD rowspan=1><A HREF="file:///home/lintz">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:36:27</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_oodc_no_log</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:36:16</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_oodc_no_outData</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:36:01</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_oodc_par</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:36:03</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_par_fail</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:50:07</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_par_qualify_dep</A></TD><TD rowspan=1>create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_parallel</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:52:49</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=4><A HREF="method.pl?path=/home/lintz/">task_template_qualify_dep</A></TD><TD rowspan=4>create</TD><TD NOWRAP>method1</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method2</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method3</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>parallel</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=3><A HREF="method.pl?path=/home/lintz/">task_template_qualify_dep_options</A></TD><TD rowspan=3>create</TD><TD NOWRAP>method1</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method2</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method3</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_resource_requ</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:40:09</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_result_checks</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:37:32</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_result_checks_exec_fail</A></TD><TD rowspan=1><A HREF="file:///home/lintz/">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:37:56</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz/">task_template_result_checks_res_fail</A></TD><TD rowspan=1><A HREF="file:///">create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP><FONT COLOR="red">IN</FONT></TD><TD NOWRAP>CO</TD><TD NOWRAP>CO</TD><TD NOWRAP>Wed Dec 8 2010 12:42:10</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=3><A HREF="method.pl?path=/home/lintz/">task_template_soft_dep</A></TD><TD rowspan=3>create</TD><TD NOWRAP>method1</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method2</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD NOWRAP>method3</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
<TR><TD rowspan=1><A HREF="method.pl?path=/home/lintz">task_template_tool_call</A></TD><TD rowspan=1>create</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>-</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD><TD NOWRAP>&nbsp;</TD></TR>
</TABLE>

<p /><br />
</body>
</html>

Philip M
06-27-2012, 01:45 PM
if (/\W\-\W/.test(mytab.rows[i].innerHTML)) {
does not match a single hyphen. It matches non-word character hyphen non-word character such as '-' or ?-?

No idea about your other problem.

xaverian
06-27-2012, 02:18 PM
Yes you are right.
"non-word character hyphen non-word character" is what it matches.
Anyways , the sytax and debugging help you mentioned surely helped.

Let me know if you can find a solution of the other problem. Thanks.

pharmaind
06-27-2012, 02:54 PM
It work perfectly for me
tested

DaveyErwin
06-27-2012, 02:59 PM
http://www.codingforums.com/showthread.php?p=1245212#post1245212

You still have the same problem
you are comparing the row
when you should be comparing
only the cell try the code in this
post

http://www.codingforums.com/showthread.php?p=1245212#post1245212

xaverian
06-27-2012, 04:01 PM
Hi Davey Erwin,

It is still not the same problem. It is another problem even after the matching of "-" is fixed. Please run the code I pasted and find that the problem I mentioned. The number of rows returned are ok but due to rowspan the third and fourth row are not coming properly after selecting "valid" as Status.

Thanks.

DaveyErwin
06-27-2012, 05:30 PM
The problem is the
"rowspans" design
your table so that
rowspans are not used.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum