1

given this text:

hello-world

I can match it with this regex:

hello\-.+?\b

The catch is if I have this:

hello-world-howyadoing

that second dash is consider a word boundary, so the match ends at 'world'

How do I say 'stop at next word boundary UNLESS that word boundary is a dash' in regex?

This is in .js, btw.

DA.
  • 39,848
  • 49
  • 150
  • 213

2 Answers2

3

I haven't done JS regex's, but I'm pretty sure underscores are actually included as well in words, so the correct set would also include a _ (I don't know if underscores need to be escaped in JS)

hello\-[a-zA-Z0-9\-_]*

Ryan
  • 3,579
  • 9
  • 47
  • 59
  • That works! Almost the same answer as Wong, but I'll have to give him the answer as he beat you to the punch. Thanks! – DA. Jun 19 '10 at 03:28
2

It depends what kind of stuff you're doing, but you can probably just specify the list of word boundaries you're interested in

hello\-[a-zA-Z0-9\-]*

Might accomplish what you want

Jamie Wong
  • 18,104
  • 8
  • 63
  • 81