3

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.

3
  • 1
    [^##] == [^#] Commented Mar 14, 2017 at 10:02
  • was not able to edit again: Shure I mean - Wanting: ["", "hello ↵##1234 ↵ ", "hello2"] Commented Mar 14, 2017 at 10:04
  • [^#] produces: ["", "ello ↵#", "234 ↵ ", "ello2"] ~ so yes / [^##] == [^#] // but how to Exclude? Commented Mar 14, 2017 at 10:08

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.