Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    55
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript match for regular expression

    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 !!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    You want to match a single hyphen and nothing but.

    Code:
    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.
    Last edited by Philip M; 06-26-2012 at 07:19 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    55
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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;
    Last edited by Philip M; 06-26-2012 at 07:21 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    May 2012
    Posts
    55
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Quote Originally Posted by xaverian View Post
    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);

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    May 2012
    Posts
    55
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

    Code:
    <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>

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    New Coder
    Join Date
    May 2012
    Posts
    55
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #10
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It work perfectly for me
    tested

  • #11
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    http://www.codingforums.com/showthre...12#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/showthre...12#post1245212

  • #12
    New Coder
    Join Date
    May 2012
    Posts
    55
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #13
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    The problem is the
    "rowspans" design
    your table so that
    rowspans are not used.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •