• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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: 5177
83
  • 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: 4813
604
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
 
Bartender
Posts: 15741
368
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic