svens
svens

Reputation: 29

javascript regex split match at char once, not multiple

Having the following example:

var s = '#hello \n##1234 \n #hello2';
var splits = s.split(/#[^##]/);

produces: ["", "ello ↵#", "234 ↵ ", "ello2"]

But want it to produce: ["hello ↵##1234 ↵ ", "hello2"]

How to match single occurrence of character (#) but NOT if its occurring multiple times, and also not the last occurrence of the multiple occurred one.

Upvotes: 3

Views: 58

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626920

With split, you would need a negative lookbehind to prevent matching # that comes after another #, but JavaScript does not support lookbehinds.

Match the chunks rather than split with .match(/(?:##+|[^#])+/g) that will match 1 or more occurrences of 2+ # or any char other than #:

var s = '#hello \n##1234 \n #hello2';
var splits = s.match(/(?:##+|[^#])+/g);
console.log(splits);

Pattern details:

  • (?:##+|[^#])+ - 1 or more occurrences (+) of:
    • ##+ - 2 or more # symbols, as many as possible
    • [^#] - any character other than # (a [^...] is a negated character class that matches any single char that is not inside the defined set/ranges).

NOTE: [^##] is the same as [^#], you cannot negate a sequence with a negated character class, only specific chars or ranges of chars.

Upvotes: 5

Related Questions