3

I am trying to match any group of characters until a line break like this: \n. I want to be able to access all groups matched then.

The text I am using to look for my regular expression is like this :

foo foo!\n *bar bar*\n *foo :* foo bar\n*foo :* 32 foo bar\n*Bar :* 37 foo foo\n*Time :* 11:00:00-14:00:00\n*Date :* 2016-12-23\n*Foo :* \n*bar* : 06XXXXXXXX

For now, I tried multiple solutions and the best working is this regex:

/([^\\n])\w+/

but it it still not perfect. It doesn't take the special chars nor spaces into account.

Guillaume
  • 537
  • 1
  • 5
  • 12

1 Answers1

2

What you need to is match at least one (+) char that is not new-line (\n):

s = 'foo foo!\n *bar bar*\n *foo :* foo bar\n*foo :* 32 foo bar\n*Bar :* 37 foo foo\n*Time :* 11:00:00-14:00:00\n*Date :* 2016-12-23\n*Foo :* \n*bar* : 06XXXXXXXX'

console.log(s.match(/([^\n]+)/g))
Dekel
  • 60,707
  • 10
  • 101
  • 129
  • The funny thing is that your regexp would still work if you replaced the `[^\n]` with a dot, and by the way you don't need the parens either. –  Dec 23 '16 at 19:34
  • @torazaburo, there are many ways write regex things :) a dot indeed is one of them :) btw, a voteup will be much appreciated! – Dekel Dec 23 '16 at 19:53
  • You should double check it. The `\n` is indeed meaningful in character class. Run my example :) – Dekel Dec 23 '16 at 21:09