[jQuery] Re: compare variables

2009-10-01 Thread Jon Banner
make friends with regular expressions.
http://en.wikipedia.org/wiki/Regular_expression


2009/9/30 Poloman bigtrouble1...@gmail.com


 I have

 varA = com and varB=com55 or ton2 or son1.9 or whatever values...

 I want to do a comparison if the prefix of varB = com, which is varA,
 then do something. How can i write the code for this ?


[jQuery] Re: compare variables

2009-10-01 Thread Michael Geary
Regular expressions are a good friend indeed, but in this particular case
they would let you down.

You could do something like:

var re = new RegExp( '^' + varA );
if( re.test(varB) )
alert( 'prefix matches' );

But what if varA contains special characters that are significant in a
regular expression, such as ? or $ or the like? Then it wouldn't do what you
want.

Instead, I would use indexOf:

if( varB.indexOf(varA) == 0 )
alert( 'prefix matches' );

BTW, Poloman, if you're wondering why you couldn't find anything in jQuery
to help with this, that's because jQuery is not a string or regular
expression library. jQuery deals primarily with the DOM and not much else.

Strings, regular expression, math functions, and so forth are all part of
JavaScript itself, so that's where you'd look for solutions.

(Don't get me wrong, no one minds having you ask here - it's just that this
kind of code isn't part of jQuery, so you won't find anything in the jQuery
docs about it.)

-Mike

On Thu, Oct 1, 2009 at 4:35 AM, Jon Banner banali...@googlemail.com wrote:

 make friends with regular expressions.
 http://en.wikipedia.org/wiki/Regular_expression


 2009/9/30 Poloman bigtrouble1...@gmail.com


 I have

 varA = com and varB=com55 or ton2 or son1.9 or whatever values...

 I want to do a comparison if the prefix of varB = com, which is varA,
 then do something. How can i write the code for this ?