Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript RegExp bug?

People's Champ ,
Feb 18, 2012 Feb 18, 2012

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

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 18, 2012 Feb 18, 2012

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,"");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Feb 18, 2012 Feb 18, 2012

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("");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 18, 2012 Feb 18, 2012

Hi Marijan -- After reading John's post, that's what I thought of as well.

Thanks,

Ariel

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2012 Feb 18, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 18, 2012 Feb 18, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 18, 2012 Feb 18, 2012
LATEST

Then use the g global as jong did… join the result

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines