0

After asking a question about it here: How to Split string with multiple rules in javascript

I got stuck in another string issue

I have this regex:

Txt.replace(/([^ ][.#:])/g, ' &$1').split(" ");

I want the condition to be: if there is "." or "#" or ":" but there is no whitespace before them then apply the condition.

so for foo#bar I will get : foo &#bar (in my array ['foo','&#bar'])

but for foo #bar nothing will happen and it will stay the same (in my array ['foo','#bar'])

The problem is that when I write foo#bar I get --> "fo&o#bar" the letters mix and no splitting has happened - it is wrong

What should I do?

thanks, Alon

Community
  • 1
  • 1
Alon
  • 7,618
  • 18
  • 61
  • 99

1 Answers1

2

You actually need two groups in your expression:

 Txt.replace(/([^ ])([.#:])/g, '$1 &$2').split(" ");
georg
  • 211,518
  • 52
  • 313
  • 390