Both Flash and JS environments return the same thing for this:
"foo".match({foo:1});

["o", index: 1, input: "foo"]0: "o"index: 1input: "foo"length: 1

It matches the first “o” in foo. I believe the reason for this is that 
Object.toString() is [object Object]. When that is evaluated to a RegExp it 
becomes a character class and matches the first character in the class which in 
my case is “o”.

I confirmed that with this:
var foo = {foo:1};
"foo".search(foo)//1
foo.toString = function(){return "baz"}
"foo".search(foo)//-1
"baz".search(foo)//0

It results in 1,-1,0 on both platforms.

So, despite what the docs say, what seems to happen on both platforms is:

1. toString() is called.
2. new RegExp() is being called with the result of that.
3. The result of #2 is being used as the parameter for match and search.

I tested this in Chrome and Firefox as well as Node.

Harbs

> On Jul 20, 2017, at 8:30 PM, Alex Harui <aha...@adobe.com.INVALID> wrote:
> 
> I just took a closer look at the ASDoc [1] and Mozilla doc [2].
> 
> Both imply that they take more than one type of input, so any utility
> function probably should as well and thus be of type "*".
> One difference in the two docs that stands out to me is in [1] where it
> says:
> 
> "If the pattern is not a regular expression or a string, then the method
> converts it to a string before executing."
> 
> But [2] says:
> 
> "If a non-RegExp object obj is passed, it is implicitly converted to a
> RegExp by using new RegExp(obj)"
> 
> 
> So, assuming this is true, then yes, we need a utility function to wrap
> and hide the differences.  I would think we want to be backward compatible
> with AS in order to ease migration effort, so I think it would look
> something like:
> 
> public function match(input:String, pattern:*):Array
> {
>  COMPILE::SWF
>  {
>    return input.match(pattern);
>  }
>  COMPILE::JS
>  {
>    if (pattern is RegExp)
>      return input.match(pattern);
>    else
>    {
>      pattern = pattern.toString();
>      if (input.indexOf(pattern) != -1)
>        return [ pattern ];
>      return null;
>    }
>  }
> }
> 
> Of course, I could be wrong...
> 
> -Alex
> 
> [1] 
> http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.h
> tml#match()
> [2] 
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob
> jects/String/match
> 
> On 7/20/17, 9:58 AM, "Harbs" <harbs.li...@gmail.com> wrote:
> 
>> Really simply:
>> new RegExp(“?”)
>> 
>> In Flash, that will produce a RegExp object which matches nothing.
>> 
>> In JS, it throws an error that the RegExp is not valid.
>> 
>> match and search seem to call the RegExp constructor under the hood. In
>> Flash it succeeds. In JS it throws an error.
>> 
>> Utility functions serve two purposes:
>> 
>> 1. It prevents the error from being thrown.
>> 2. It warns the developer to not pass in a string as it’s converted to a
>> RegExp anyway and is less predictable.
>> 
>> An alternate (possibly better) way to handle this would be to have the
>> compiler issue a warning.
>> 
>>> On Jul 20, 2017, at 7:05 PM, Alex Harui <aha...@adobe.com.INVALID>
>>> wrote:
>>> 
>>> I think I'm lost.  If both behave the same, then why do we need to call
>>> some utility function?
>>> 
>>> -Alex
>>> 
>>> On 7/20/17, 5:00 AM, "Yishay Weiss" <yishayj...@hotmail.com> wrote:
>>> 
>>>> Good catch. I was misled by the docs [1].
>>>> 
>>>> [1] 
>>>> 
>>>> https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fhelp.ado
>>>> be
>>>> 
>>>> .com%2Fen_US%2FFlashPlatform%2Freference%2Factionscript%2F3%2FString.htm
>>>> l%
>>>> 
>>>> 23match&data=02%7C01%7C%7C66b63f8228ab4f80a39408d4cf66f9a9%7Cfa7b1b5a7b3
>>>> 44
>>>> 
>>>> 38794aed2c178decee1%7C0%7C0%7C636361488561328745&sdata=CbWB8sk4vOv1vId9a
>>>> T7
>>>> WkDjQrkqarHU2aAAoWw9UBNA%3D&reserved=0()
>>>> 
>>>> From: Harbs<mailto:harbs.li...@gmail.com>
>>>> Sent: Thursday, July 20, 2017 9:40 AM
>>>> To: dev@flex.apache.org<mailto:dev@flex.apache.org>
>>>> Subject: Re: [FlexJS] String.match()
>>>> 
>>>> Both Flash and JS engines automatically convert to RegExp. I had not
>>>> realized that at first.
>>>> 
>>>> It seems that the only difference between the Flash engine and JS
>>>> engines
>>>> is what happens when constructing a RegExp object from invalid input.
>>>> Flash matches nothing, while JS throws an error.
>>>> 
>>>> By just wrapping the call in a try/catch, that seems to resolve the
>>>> issue.
>>>> 
>>>> I added the trace because I think it’s bad practice to use strings
>>>> instead of RegExp because it can lead to unexpected results and a new
>>>> RegExp instance needs to be constructed every time.
>>>> 
>>>>> On Jul 20, 2017, at 3:32 AM, Alex Harui <aha...@adobe.com.INVALID>
>>>>> wrote:
>>>>> 
>>>>> It looks like they just trace a warning instead of trying to convert
>>>>> to
>>>>> a
>>>>> RegExp.  Is that what we want to do?  Or should we add code that
>>>>> converts
>>>>> a string to regex?
>>>>> 
>>>>> Thoughts?
>>>>> -Alex
>>>>> 
>>>>> On 7/19/17, 4:32 AM, "Harbs" <harbs.li...@gmail.com> wrote:
>>>>> 
>>>>>> I added the utility functions. I think they can be very simple.
>>>>>> 
>>>>>>> On Jul 19, 2017, at 9:30 AM, Alex Harui <aha...@adobe.com.INVALID>
>>>>>>> wrote:
>>>>>>> 
>>>>>>> Unless we are absolutely sure that everybody will need the code
>>>>>>> generated
>>>>>>> by the compiler, having the compiler call a framework function makes
>>>>>>> it
>>>>>>> easier for an app developer to make any adjustments to that code.
>>>>>>> It
>>>>>>> is
>>>>>>> easier to monkey-patch a utility function than find-and-replace some
>>>>>>> sequence of code the compiler has sprinkled throughout the output.
>>>>>>> 
>>>>>>> My 2 cents,
>>>>>>> -Alex
>>>>>>> 
>>>>>>> On 7/18/17, 11:36 AM, "yishayw" <yishayj...@hotmail.com> wrote:
>>>>>>> 
>>>>>>>> Alex Harui-2 wrote
>>>>>>>>> By
>>>>>>>>> calling new utility functions, the developer has control over the
>>>>>>>>> conversion.
>>>>>>>> 
>>>>>>>> I don't understand that point. Do you mean an app developer?
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> --
>>>>>>>> View this message in context:
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fapac
>>>>>>>> he
>>>>>>>> -f
>>>>>>>> le
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> x-development.2333347.n4.nabble.com%2FFlexJS-String-match-tp63392p63
>>>>>>>> 40
>>>>>>>> 5.
>>>>>>>> ht
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> ml&data=02%7C01%7C%7C9ff1088402ef439ca27908d4ce0e97eb%7Cfa7b1b5a7b34
>>>>>>>> 43
>>>>>>>> 87
>>>>>>>> 94
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> aed2c178decee1%7C0%7C0%7C636360009448210304&sdata=nprl9yHUtlsTHbIZxe
>>>>>>>> Fq
>>>>>>>> 2h
>>>>>>>> %2
>>>>>>>> FQNWmtimM%2BxAt0kJA8EcA%3D&reserved=0
>>>>>>>> Sent from the Apache Flex Development mailing list archive at
>>>>>>>> Nabble.com.
>>>>>>> 
>>>>>> 
>>>>> 
>>>> 
>>> 
>> 
> 

Reply via email to