www.webdeveloper.com
Results 1 to 5 of 5

Thread: Match() not finding string

  1. #1
    Join Date
    Jul 2009
    Posts
    3

    Question Match() not finding string

    Hello,

    I am a JavaScript newbie with a Match() issue that I was hoping someone can help me with.

    The string I am trying to match to is: ****[( S )][( I )]****

    The code I have used to match the string is:

    if (name.match([/\sS\s/])==[/\sS\s/]) return true;

    It does not throw any errors, but it also does not match the string. Any and all assistance you can provide how I can code the above line, would be greatly appreciated.

    The following is additional background and steps I took to try to resolve the problem:

    I used http://www.regexplanet.com/simple/index.jsp to test the code and it showed no match although it shows that it works with replaceFirst(), replaceAll() and find().

    The code is part of a function that works perfectly except for this line. Part of the other lines in this function are:

    function skipchallenge(name)
    {

    if (name.match([/\sS\s/])==[/\sS\s/]) return true;

    if (name.match("CLOSED")=="CLOSED") return true;
    if (name.match("VOID")=="VOID") return true;
    if (name.match("^WINNER")=="WINNER") return true;

    return true;
    }

    Some of the lines that I have tried (more like grasping at straws ) include:

    if (name.match([/\\sS\\s/])==[/\\sS\\s/]) return true;
    if (name.match("\s(?=S)\s")=="\s(?=S)\s") return true;
    if (name.match("\\s(?=S)\\s")=="\\s(?=S)\\s") return true;
    if (name.match("' '(?=S)' '")=="' '(?=S)' '") return true;
    if (name.match("\*{4}")=="\*{4}) return true;
    if (name.match("\*\*\*\*\[\(")=="\*\*\*\*\[\(") return true;
    if (name.match("\\sS\\s")=="\\sS\\s") return true;
    if (name.match("S \)\]\[\( I")==("S \)\]\[\( I") return true;

    Kind regards,
    Richard

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    99% of them are not valid regular expressions.

    A regular expression starts with / and ends with /

    so if you want to match a sting with abc it would be

    Code:
    /abc/
    if you want to match the character [, (, ), or ] than you need to escape them. So if you have the string

    Code:
    var a = "a[(b)]c";
    and you want to match it, your regular expression would look like

    Code:
    /a\[\(b\)\]c/
    Find yourself an online reference for regular expressions and read through it.

    Eric

  3. #3
    Join Date
    Jul 2009
    Posts
    3
    Thank you Eric for your comments. I have been searching and reading the web for the past week and it has successfully gotten me through 590 lines of code. My only issue is this one line and I am finished. I am probably just missing it, but I just cannot figure out what I am doing wrong in this last piece. Any help anyone can provide with the proper syntax would be greatly appreciated.

    Kind regards,
    Richard

  4. #4
    Join Date
    May 2007
    Posts
    129
    Code:
    function skipchallenge(name)
    {
    
    	if (name.match(/\[\(\sS\s\)\]/) return true;
    
    	if (name.match("CLOSED")=="CLOSED") return true;
    	if (name.match("VOID")=="VOID") return true;
    	if (name.match("^WINNER")=="WINNER") return true;
    
    	return true;
    }

  5. #5
    Join Date
    Jul 2009
    Posts
    3
    Quote Originally Posted by web_bert View Post
    Code:
    	if (name.match(/\[\(\sS\s\)\]/) return true;
    Thank you Web_Bert for the suggestion. Unfortunately it did not work. I did add a final ) before the "return true" (to close the if "("), but it still did not work.

    I have continued to read on expressions and cannot figure out why something like the following does not work:

    if (name.match(/\sS\s/)) return true;

    If any one can shed light why the above does not work or provide suggestions as to what might work, it would be greatly appreciated.

    Richard

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles