|
-
Jan 15th, 2003, 11:45 AM
#1
Thread Starter
PowerPoster
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.
-
Jan 15th, 2003, 11:49 AM
#2
Black Cat
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.
-
Jan 15th, 2003, 11:51 AM
#3
Thread Starter
PowerPoster
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
-
Jan 15th, 2003, 12:10 PM
#4
Thread Starter
PowerPoster
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>
-
Jan 15th, 2003, 12:42 PM
#5
Thread Starter
PowerPoster
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>
-
Jan 15th, 2003, 12:49 PM
#6
Thread Starter
PowerPoster
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>
-
Jan 16th, 2003, 06:53 AM
#7
Fanatic Member
He he, I love it when people answer their own posts
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|