Results 1 to 7 of 7

Thread: Resolved - Evaluate the first 4 characters in a string?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Question Resolved - Evaluate the first 4 characters in a string?

    How can I do this?

    Let's say the string..
    Code:
    var strString = "12.This is it"
    How can I test if the first 2 chars are numbers and then there is a [.] after it?

    Thanks
    Last edited by jesus4u; Jan 15th, 2003 at 12:49 PM.

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    I'd use regular expressions (Javascript in newer browsers has Perl style RegEx syntax, same as VBScript).

    Code:
    if ( /^\d{2}\./.test(strString) ) {
    
    }
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by JoshT
    I'd use regular expressions (Javascript in newer browsers has Perl style RegEx syntax, same as VBScript).

    Code:
    if ( /^\d{2}\./.test(strString) ) {
    
    }
    I appreciate that. But the strings will not always be the same. It will be like this:

    1.sdfsddsfsdf
    2.dfsdfsdfdsf
    3.sdfsdf

    etc...

    Thanks

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Why is the code still finding a match for the [.] when it is not in the string?

    Code:
    <SCRIPT LANGUAGE=javascript>
    <!--
    	var strString = "12this is it"
    	if ( /^\d|\d\./.test(strString) ) {
    		alert(strString)
    	} 
    
    //-->
    </SCRIPT>

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    How can I look for either occurence of 12 or 1 in the string?
    Because so far only will find 2 occurences first.
    Code:
    <SCRIPT LANGUAGE=javascript>
    <!--
    	// / beginning and end of what is evaluated
    	// \d = [0-9]
    	// \s =  space
    	// \ separator
    	
    	var strString = "1. this is it"
    	if ( /^\d|\d\.\s/.test(strString) ) {
    		alert(strString)
    	} 
    
    //-->
    </SCRIPT>

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    NEver mind , got it!
    Code:
    <SCRIPT LANGUAGE=javascript>
    <!--
    	// / beginning and end of what is evaluated
    	// \d = [0-9]
    	// \s =  space
    	// \ separator
    	
    	var strString = "12. this is it"
    	if ( /^\d{1,2}\.\s/.test(strString) ) {
    		alert(strString)
    	} 
    
    //-->
    </SCRIPT>

  7. #7
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    He he, I love it when people answer their own posts
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

Posting Permissions

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



Click Here to Expand Forum to Full Width