PDA

Click to See Complete Forum and Search --> : Resolved - Evaluate the first 4 characters in a string?


jesus4u
Jan 15th, 2003, 10:45 AM
How can I do this?

Let's say the string..

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

JoshT
Jan 15th, 2003, 10:49 AM
I'd use regular expressions (Javascript in newer browsers has Perl style RegEx syntax, same as VBScript).


if ( /^\d{2}\./.test(strString) ) {

}

jesus4u
Jan 15th, 2003, 10:51 AM
Originally posted by JoshT
I'd use regular expressions (Javascript in newer browsers has Perl style RegEx syntax, same as VBScript).


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

jesus4u
Jan 15th, 2003, 11:10 AM
Why is the code still finding a match for the [.] when it is not in the string?



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

//-->
</SCRIPT>

jesus4u
Jan 15th, 2003, 11:42 AM
How can I look for either occurence of 12 or 1 in the string?
Because so far only will find 2 occurences first.

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

jesus4u
Jan 15th, 2003, 11:49 AM
NEver mind , got it!

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

punkpie_uk
Jan 16th, 2003, 05:53 AM
He he, I love it when people answer their own posts ;)