Javascript RegExp bug?
Copy link to clipboard
Copied
Hi,
I'm sending this via email (no Internet access right now) so I hope it
comes through properly
In the ExtendScript console, try this:
"hello".match(/[a-z]+/)
The output is
hello
which is correct
But if you try this:
"hello".match(/[f-z]+/)
the output is only
h
which is not what I would have expected. Shouldn't + match all
characters that are within the range f-z?
Can anyone help me understand this, or is it in fact a bug?
Thanks,
Ariel
Copy link to clipboard
Copied
Ariel:
Some portion of your post was misformatted, because Jive turns square brackets into hyperlinks, but I don't think we lost anything.
This behavior is unambiguously correct. [f-z] matches a single character in the class, and + tells it to match one or more of the previous, in a contiguous block. Since 'h' matches and 'e' does not, it stops with 'h'.
You haven't told us what you expected, or what you are trying to achieve, so it's a bit tough to give you the answer you're looking for.
If you are expecting to return the non-contiguous set of characters in the string that match a class, then regexps don't really work that way. You could, I suppose, replace all characters in the inverted class [^f-z] with the null string, which would have a similar effect. i.e. "hello".replace(/[^f-z]/g,"");
Copy link to clipboard
Copied
John Hawkinson wrote:
If you are expecting to return the non-contiguous set of characters in the string that match a class, then regexps don't really work that way. You could, I suppose, replace all characters in the inverted class [^f-z] with the null string, which would have a similar effect. i.e. "hello".replace(/[^f-z]/g,"");
Or simplier: "hello".match(/[f-z]+/g).join("");
Copy link to clipboard
Copied
Hi Marijan -- After reading John's post, that's what I thought of as well.
Thanks,
Ariel
Copy link to clipboard
Copied
John Hawkinson wrote:
This behavior is unambiguously correct. [f-z] matches a single character in the class, and + tells it to match one or more of the previous, in a contiguous block. Since 'h' matches and 'e' does not, it stops with 'h'.
It does work correct, as this
"hello".match(/[f-z]+/g)
returns two strings: "h,llo" -- exactly as specified in that range
Copy link to clipboard
Copied
Hi John,
Thanks for your response. Sorry for the formatting problems, but I think
you did indeed decipher what came through correctly!
I forgot that replace will only return a contiguous block. Now I
remember that I've got to use arrays.
What I was hoping was to extract from a given string only the letters I
want: from "hello" everything but the e, for instance: "hllo"
Your inverted idea should work, and also using a "g" tag at the end like
you do, with a join("") should do the trick.
Thanks for your help. I'll try and remember to mark the answer as
correct when I have the chance.
Ariel
Copy link to clipboard
Copied
Then use the g global as jong did… join the result

