• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stumped: JavaScript match method regular expression

 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With this string "x | AAA BBB cc dddd ... |", where between the vertical pipes is an arbitrary combination of characters and spaces, I tried

    match(/\|[\/S ]+?\|/), which did not, and should not, match.

But then I tried

    match(/\|[\/S ]+\|/) which did not match, much to my surprise, given the greedy nature of the + quantifier.

So then I tried

    match(/\|[\/S ]+(?=\|)/), which did not match either.

Thanks in advance for any insight you can provide.
 
Master Rancher
Posts: 4831
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you describe in words what you expect

[\/S ]

to mean?  

To me, it looks like a character class that includes the characters '/' (unnecessarily escaped), 'S', and ' '.  I don't see any reason to expect why that would work here.

Perhaps you're trying to use \S to indicate a non-whitespace character?  That wouldn't work inside a character class marked with [ ].  Maybe try

match(/\|(\S| )*?\|/)
 
Marshal
Posts: 4514
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe  /\|[^|]+\|/

I tried it in Chrome's dev mode console and it seems to work
 
Saloon Keeper
Posts: 15535
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:


As a side note, I think this is a wonderful example of how JavaScript's weak type system compounds with the poorly named "match" method to lead to a truly surprising result.
 
John La
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for providing clarity. I had at least two competing mistakes preventing the solution to my problem, slash-backslash being one of them. Needless to say, I've got a re-occurring problem with substr-substring methods too.

It's like learning a non-specific foreign language: it's simple, not necessarily easy.
 
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic