startsWith() and endsWith() - Printable Version -Shoutbox (https://shoutbox.menthix.net) +-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58) +--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4) +---- Forum: Scripting (/forumdisplay.php?fid=39) +----- Thread: startsWith() and endsWith() (/showthread.php?tid=91093) startsWith() and endsWith() by Flippy on 06-17-2009 at 04:59 PM
Hey, jscript code:But it didn't work. They are returning false while they should be true. I figured maybe the regex works differently in jscript, and I was stuck there... I didn't know how to convert it to jscript properly. So I decided to just make it work for my purpose only, which is to see if the orig string starts or ends with the string "$$". So I made it into this: jscript code: Now, the startsWith function works fine, but the endsWith method does not... I suppose my regex is faulty, especially with the many $ signs lol... The last $ sign appears to mean something like "at the end" (I guess, I'm really rusty at regex), but I also need it to check the string for actual $ characters... Can anyone help me out? This shouldn't be too hard, but unfortunately jscript is a little... well let's just say I don't like it too much hehe. RE: startsWith() and endsWith() by Mnjul on 06-17-2009 at 05:28 PM
Maybe this will work.. jscript code: I'm assuming str won't be longer than orig. If str's length is exactly one char more than orig's length, then orig.length-str.length returns -1 which evaluates the expression to true. RE: RE: startsWith() and endsWith() by Flippy on 06-17-2009 at 06:26 PM
quote: Thanks. I didn't try it yet (will do later tonight), but I cannot guarantee that orig is longer than str. str will actually be "$$" always (at least in my script), and orig is simply the message someone receives. That message might be a simple "k" or something like that, in which case this would fail (right?). I suppose I can always use the indexOf function to see if the message contains any "$$" in the first place, before continuing and doing this. That would probably solve the problem. One more thing... What the hell is a triple "=" ? How does this even work lol. RE: startsWith() and endsWith() by CookieRevised on 06-17-2009 at 07:19 PM
quote:Fixed by using: js code: ---------------------- For that triple '=', those are identity operators (opposed to a double '=' which is a equality operator). In almost all cases it is better to use identity operators instead of equality operators, unless there is a specific reason you need equality instead of identity. see CookieRevised's reply to Script about lock messenger. or look it up in the Windows Script Documentation > Index > Comparison operators > JScript5.6 RE: startsWith() and endsWith() by Flippy on 06-17-2009 at 07:46 PM
I see, so it has to do with type conversion. RE: startsWith() and endsWith() by CookieRevised on 06-17-2009 at 07:52 PM
quote:Correct. And this is the same in VBScript and JavaScript btw. But even in C#, VB, etc you have that kind of type, usually it is called a Variant. However, although JScript has a very loose type convention (which can be handy, but also prone to user errors), you can define variables as a specific type using the New operator. In fact, to be more correct, you actually define the variable as an object of a certain type. So your variable actually becomes an object. eg: var myBoolean = new boolean(); However, the moment you start comparing normal variables, JScript internally converts the variables to the proper types. Hence the existance and difference between equality and identity operators. In fact, you actually provided a nice example yourself to explain it. See below (the fine print): ----------------------------- Anywyas, in regards to your reg. expression: quote:Because you actually check if the string is exactly "$$", nothing more, nothing less. This because you have the ^ in there (meaning 'from the beginning'). So, the correct expression is: jscript code:PS: in regards to the identity and equality issue. Here we use == instead of === because match actually returns an array, and you compare it to a string. So these are two different types. If you would use === then the comparisson would always return false because the two things you compare are of a different type (internaly). Using == makes that JScript internally first converts everything to a string (in this case). Thus it first converts the array to a string prior to comparing. When doing so, it simply takes the first array element disgarding all the rest and converts that into a string. But better would be using the test method instead. Because that does not require the internal reg. exp. object to update, it doesn't need to create and populate an array, and already returns a boolean (so JScript doesn't need to converts types either). And thus it also runs a lot faster: jscript code: RE: startsWith() and endsWith() by markee on 06-18-2009 at 07:46 AM
I normally am a person who uses regular expressions all over the place (it is my job after all) but in this case I would highly recommend not doing it as it is a much slower option. Instead I would use the following JScript code: I am not sure if a single character string is equivalent to a character, that is why I didn't use "===". RE: startsWith() and endsWith() by CookieRevised on 09-22-2009 at 08:15 AM
quote:yup, it is. Both are 'strings'. RE: startsWith() and endsWith() by brooooof on 09-27-2009 at 06:43 AM
Thanks RE: startsWith() and endsWith() by jerone on 10-08-2009 at 11:40 AM
I don't know if this works in Jscript: JScript code: |