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

1 Answer 1

2

You actually need two groups in your expression:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.